Skip to content

Commit 8b10dc2

Browse files
committed
build dictionary from list of words
1 parent df80b1a commit 8b10dc2

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

google-python-exercises/basic/wordcount.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,34 @@
4444
# You could write a helper utility function that reads a file
4545
# and builds and returns a word/count dict for it.
4646
# Then print_words() and print_top() can just call the utility function.
47-
def read_file(filename)
47+
48+
#Consume list and produce dict
49+
def build_dict(filename):
4850

4951
f = open(filename, 'rU')
5052
words = f.read().split()
51-
return words
52-
53-
def print_words(filename)
5453

55-
read_file(filename)
54+
dict = {}
55+
while len(words) > 0:
56+
word = words[0]
57+
dict[word]=words.count(word)
58+
while word in words:
59+
words.remove(word)
60+
return dict
61+
62+
def print_words(filename):
63+
64+
dict = build_dict(filename)
65+
66+
sorted(dict)
67+
for k in dict.keys():
68+
print k + " " + str(dict[k])
69+
5670
#
57-
###
58-
71+
def print_top(filename):
72+
words = []
73+
dict = {}
74+
5975
# This basic command line argument parsing code is provided and
6076
# calls the print_words() and print_top() functions which you must define.
6177
def main():

0 commit comments

Comments
 (0)