From 0311a99b4753f2432126652baaca78b844180d02 Mon Sep 17 00:00:00 2001 From: Crista Lopes Date: Tue, 18 Mar 2014 16:44:31 -0700 Subject: [PATCH] Made the recursive program a bit simpler --- 07-infinite-mirror/tf-07.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/07-infinite-mirror/tf-07.py b/07-infinite-mirror/tf-07.py index f41317a..9c281a1 100755 --- a/07-infinite-mirror/tf-07.py +++ b/07-infinite-mirror/tf-07.py @@ -11,29 +11,24 @@ def count(word_list, stopwords, wordfreqs): # What to do with an empty list if word_list == []: return - # The base case, what to do with 1 word - if len(word_list) == 1: + # The inductive case, what to do with a list of words + else: + # Process the head word word = word_list[0] if word not in stopwords: if word in word_freqs: wordfreqs[word] += 1 else: wordfreqs[word] = 1 - # The inductive case, what to do with a list of words - else: - # Process the head word - count([word_list[0]], stopwords, wordfreqs) # Process the tail count(word_list[1:], stopwords, wordfreqs) def wf_print(wordfreq): if wordfreq == []: return - if len(wordfreq) == 1: + else: (w, c) = wordfreq[0] print w, '-', c - else: - wf_print([wordfreq[0]]) wf_print(wordfreq[1:]) stop_words = set(open('../stop_words.txt').read().split(','))