File tree Expand file tree Collapse file tree 1 file changed +15
-8
lines changed
exercises/41-frequency-of-words Expand file tree Collapse file tree 1 file changed +15
-8
lines changed Original file line number Diff line number Diff line change 1
- freq = {} # frequency of words in text
2
- line = input ()
3
- for word in line .split ():
4
- freq [word ] = freq .get (word ,0 )+ 1
1
+ # Your code here
2
+ def compute_word_frequency (sentence ):
3
+ words = sentence .split ()
5
4
6
- words = freq .keys ()
7
- words .sort ()
5
+ word_frequency = {}
8
6
9
- for w in words :
10
- print ("%s:%d" % (w ,freq [w ]))
7
+ for word in words :
8
+ word_frequency [word ] = word_frequency .get (word , 0 ) + 1
9
+
10
+ sorted_word_frequency = sorted (word_frequency .items (), key = lambda x : x [0 ])
11
+
12
+ for word , frequency in sorted_word_frequency :
13
+ print (f"{ word } : { frequency } " )
14
+
15
+
16
+ input_sentence = "New to Python or choosing between Python 2 and Python 3? Read Python 2 or Python 3."
17
+ compute_word_frequency (input_sentence )
You can’t perform that action at this time.
0 commit comments