Word Count - 2
Given a sentence S, write a program to print the frequency of each word in S, where words are sorted in alphabetical order.
Input
The input will be a single line containing a string S.
Output
The output contains multiple lines, with each line containing a word and frequency of each word in the given string separated by ": ", where words are sorted in alphabetical order.
Explanation
For example, if the given sentence is "Hello world, welcome to python world", the output should be
Hello: 1
python: 1
to: 1
welcome: 1
world: 2
import string
chars = string.ascii_letters
freq = dict()
s = ""
for c in input().lower() + ' ':
if c in chars:
s += c
elif c == ' ':
if s in freq.keys():
freq[s] += 1
else:
freq[s] = 1
s = ""
for k,v in freq.items():
print("{0} : {1}".format(k,v))
Comments
Leave a comment