In: Computer Science
Your assignment is to build a program that can take a string as input and produce a “frequency list” of all of the words in the string (see the definition of a word below.) For the purposes of this assignment, the input strings can be assumed not to contain escape characters (\n, \t, …) and to be readable with a single input() statement. When your program ends, it prints the list of words. In the output, each line contains of a single word and the number of times that word occurred in the input. For readability, the number should be the first thing on the line and the word should be second.
Specific programming requirements
1. Use good prompts for all user input
2. No “dead code” – remove all diagnostic prints, abandoned attempts, etc.
3. Use loops and data container types to reduce repeated code. No section of the program should be longer than necessary and the whole program must be less than 150 lines long.
4. Use comments only when necessary to document the program.
5. Variable names must be mnemonic
6. The first lines in the program must be a comment(s) containing your name, class and section
7. Your program must end normally (not crash or get stuck in an infinite loop)
# your name= ,class= ,section=
def frequency(str):
# split() is used to split a string into a list where each word is a list item
str = str.split()
str2 = []
for i in str:
#inserting words in str2[] ,no duplicate values are inserted
if i not in str2:
str2.append(i)
#checking how many times each word in str2 is repeated and printing the word and it's frequency
for i in range(0, len(str2)):
print(str.count(str2[i]),' ',str2[i])
str =input("Enter the String : ")
frequency(str)
i am sharing the screenshot of code in the ide for your reference for seeing indendation of the code:
output: