File tree Expand file tree Collapse file tree 1 file changed +23
-7
lines changed
google-python-exercises/basic Expand file tree Collapse file tree 1 file changed +23
-7
lines changed Original file line number Diff line number Diff line change 44
44
# You could write a helper utility function that reads a file
45
45
# and builds and returns a word/count dict for it.
46
46
# 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 ):
48
50
49
51
f = open (filename , 'rU' )
50
52
words = f .read ().split ()
51
- return words
52
-
53
- def print_words (filename )
54
53
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
+
56
70
#
57
- ###
58
-
71
+ def print_top (filename ):
72
+ words = []
73
+ dict = {}
74
+
59
75
# This basic command line argument parsing code is provided and
60
76
# calls the print_words() and print_top() functions which you must define.
61
77
def main ():
You can’t perform that action at this time.
0 commit comments