Respuesta :
Answer:
- n = input("Give your input: ")
- word_list = n.split(" ")
- count = int(word_list[0])
- word_dict = {}
- counter = 0
- for i in range(1, count):
- if(word_list[i] not in word_dict):
- word_dict[word_list[i]] = 1
- else:
- word_dict[word_list[i]] += 1
- print(word_dict)
Explanation:
The solution code is written in Python 3.
Firstly, we prompt user to input a list of words started with an integer (e.g. 5 apple banana orange apple grape ) (Line 1).
Next, we use the split method to break the input into a list of individual words (Line 3). We extract the first element of the list and assign it to count variable (Line 4).
Create a word dictionary (Line 6) and a counter (Line 7). Create a for loop to traverse through the number from 1 to count (Line 9). If the word indexed by current i-value is not found in the word dictionary, create a new key in the dictionary using the current string and set frequency 1 to it (Line 9-10). Otherwise, use the current string as the key and increment its value by one (Line 11 - 12).
Print the word dictionary and we shall get {'apple': 2, 'banana': 1, 'orange': 1}
The program outputs the frequency of words inputted by the user. The program written in python 3 goes thus :
length = int(input('number of words : '))
#prompts user tonentwr the number of words to be supplied
count = 0
#takes note of the number of words inputted
word_list =[]
#holds the inputted words
while count < length:
#ensures that number of words does not exceed the number specified
word = input('Enter word : ')
#prompts user for input
word_list.append(word)
#append each word to the list
count+=1
#inceease count by 1
def CountFreq(lis):
#initialize a function
freq = {}
#initialize an empty dictionary
for item in lis:
#iterate through the list parameter
if (item in freq):
freq[item] += 1
#increase frequency by 1 if value already exists
else:
freq[item] = 1
#if not set the frequency to 1
print(freq)
# display the frequency
CountFreq(word_list)
#A sample run of the program ls attached
Learn more :https://brainly.com/question/15545822
