-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion22.py
More file actions
36 lines (30 loc) · 797 Bytes
/
question22.py
File metadata and controls
36 lines (30 loc) · 797 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'''
Question 22
Level 3
Question:
Write a program to compute the frequency of the words from the input. The output should output after sorting the key alphanumerically.
Suppose the following input is supplied to the program:
New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3.
Then, the output should be:
2:2
3.:1
3?:1
New:1
Python:5
Read:1
and:1
between:1
choosing:1
or:2
to:1
Hints
In case of input data being supplied to the question, it should be assumed to be a console input.
'''
consoleInput = input("Enter the sentence to find the frequency of words : ")
line = consoleInput.strip().split(" ")
freq = {} # declaring dict
for word in line:
freq[word] = freq.get(word, 0) + 1
words = freq.keys()
for w in words:
print("%s : %d" % (w, freq[w]))