From 7168921e91928b1ac6689002070e3b380aca8ff4 Mon Sep 17 00:00:00 2001 From: Darius Bacon Date: Fri, 29 Nov 2013 20:57:17 -0300 Subject: [PATCH 1/2] A shorter golfed version. --- 04-code-golf/tf-04-pn.py | 0 04-code-golf/tf-04.py | 10 ++++------ 2 files changed, 4 insertions(+), 6 deletions(-) mode change 100644 => 100755 04-code-golf/tf-04-pn.py diff --git a/04-code-golf/tf-04-pn.py b/04-code-golf/tf-04-pn.py old mode 100644 new mode 100755 diff --git a/04-code-golf/tf-04.py b/04-code-golf/tf-04.py index 668fedd..4dcce5e 100755 --- a/04-code-golf/tf-04.py +++ b/04-code-golf/tf-04.py @@ -1,8 +1,6 @@ #!/usr/bin/env python -import re, string, sys +import heapq, re, sys -stops = set(open("../stop_words.txt").read().split(",") + list(string.ascii_lowercase)) -words = [x.lower() for x in re.split("[^a-zA-Z]+", open(sys.argv[1]).read()) if len(x) > 0 and x.lower() not in stops] -unique_words = list(set(words)) -unique_words.sort(lambda x, y: cmp(words.count(y), words.count(x))) -print "\n".join(["%s - %s" % (x, words.count(x)) for x in unique_words[:25]]) +words = re.findall("[a-z]{2,}", open(sys.argv[1]).read().lower()) +for w in heapq.nlargest(25, set(words) - set(open("../stop_words.txt").read().split(",")), words.count): + print w, "-", words.count(w) From 0ca3d85f5a7e88b43d2502b00e9c79ac19d45497 Mon Sep 17 00:00:00 2001 From: Darius Bacon Date: Fri, 29 Nov 2013 20:59:41 -0300 Subject: [PATCH 2/2] Make Norvig's submission work with the test script. (It was skipped, plus it would fail without the #!.) --- 04-code-golf/tf-04-pn.py | 1 + 1 file changed, 1 insertion(+) diff --git a/04-code-golf/tf-04-pn.py b/04-code-golf/tf-04-pn.py index 87c6b3e..3071f64 100755 --- a/04-code-golf/tf-04-pn.py +++ b/04-code-golf/tf-04-pn.py @@ -1,3 +1,4 @@ +#!/usr/bin/env python # My golf score is slightly lower! # Best wishes, Peter Norvig