forked from learning-zone/python-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmost_frequent_word.py
44 lines (27 loc) · 944 Bytes
/
most_frequent_word.py
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
37
38
39
40
41
42
43
44
def most_frequent_word(str):
""" Find k most frequent words in a string of words, and print them in space-separated alphabetical order.
>>> most_frequent_word('hello my name is hello joanne')
hello
>>> most_frequent_word('hello my name is hello joanne is')
hello is
>>> most_frequent_word('hello my name is joanne')
hello is joanne my name
"""
# time: O(n log n)
# space: O(n)
words = {}
list_of_words = str.split()
for word in list_of_words:
words[word] = words.get(word, 0) + 1
most_frequent_words = []
max_value = max(words.values())
for word, value in words.iteritems():
if value == max_value:
most_frequent_words.append(word)
for word in sorted(most_frequent_words):
print word,
if __name__ == '__main__':
import doctest
results = doctest.testmod()
if not results.failed:
print 'All tests passed!'