From fdf9a855f2bc491d3098005c86fb0606cba9a72e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas?= Date: Thu, 16 Jan 2014 21:45:45 -0800 Subject: [PATCH 1/2] Using function composition to print the results as well. --- 05-candy-factory/tf-05.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/05-candy-factory/tf-05.py b/05-candy-factory/tf-05.py index 5bc4ccd..c239151 100755 --- a/05-candy-factory/tf-05.py +++ b/05-candy-factory/tf-05.py @@ -60,11 +60,13 @@ def sort(word_freq): """ return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True) +def print_all(word_freqs): + if(len(word_freqs) > 0): + print word_freqs[0][0], ' - ', word_freqs[0][1] + print_all(word_freqs[1:]); + # # The main function # -word_freqs = sort(frequencies(remove_stop_words(scan(filter_chars_and_normalize(read_file(sys.argv[1])))))) - -for tf in word_freqs[0:25]: - print tf[0], ' - ', tf[1] +print_all(sort(frequencies(remove_stop_words(scan(filter_chars_and_normalize(read_file(sys.argv[1]))))))[0:25]) From 0ffe4b3fd6b568e4da3928fdae5bbeb8caa7898d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas?= Date: Thu, 16 Jan 2014 21:47:54 -0800 Subject: [PATCH 2/2] Adding documentation for print_all --- 05-candy-factory/tf-05.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/05-candy-factory/tf-05.py b/05-candy-factory/tf-05.py index c239151..79c916b 100755 --- a/05-candy-factory/tf-05.py +++ b/05-candy-factory/tf-05.py @@ -61,6 +61,9 @@ def sort(word_freq): return sorted(word_freq.iteritems(), key=operator.itemgetter(1), reverse=True) def print_all(word_freqs): + """ + Takes a list of pairs where the entries are sorted by frequency and print them recursively. + """ if(len(word_freqs) > 0): print word_freqs[0][0], ' - ', word_freqs[0][1] print_all(word_freqs[1:]);