From 46536c18e58bc670f2f3d8d65b4ef706436d641d Mon Sep 17 00:00:00 2001 From: Crista Lopes Date: Sun, 13 Oct 2013 20:47:33 -0700 Subject: [PATCH] Added infinite mirror style --- 07-infinite-mirror/tf-07.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 07-infinite-mirror/tf-07.py diff --git a/07-infinite-mirror/tf-07.py b/07-infinite-mirror/tf-07.py new file mode 100644 index 0000000..2f1f73a --- /dev/null +++ b/07-infinite-mirror/tf-07.py @@ -0,0 +1,30 @@ +import re, sys, operator + +# Mileage may vary. If this crashes, make it lower +RECURSION_LIMIT = 9500 +# We add a few more, because, contrary to the name, +# this doesn't just rule recursion: it rules the depth of the call stack +sys.setrecursionlimit(RECURSION_LIMIT+10) + +def count(word_list, word_freqs): + if word_list == []: + return + + if word_list[0] not in stopwords: + if word_list[0] in word_freqs: + word_freqs[word_list[0]] += 1 + else: + word_freqs[word_list[0]] = 1 + + count(word_list[1:], word_freqs) + +stopwords = set(open('../stop_words.txt').read().split(',')) +words = re.findall('[a-z]{2,}', open(sys.argv[1]).read().lower()) +word_freqs = {} +# Theoretically, we would just call count(words, word_freqs) +# Try doing that and see what happens. +for i in range(0, len(words), RECURSION_LIMIT): + count(words[i:i+RECURSION_LIMIT], word_freqs) + +for (w, c) in sorted(word_freqs.iteritems(), key=operator.itemgetter(1), reverse=True)[:25]: + print w, '-', c