From a5e05c411c229b128f56eddd027efc05230778f9 Mon Sep 17 00:00:00 2001 From: bmistree Date: Fri, 20 May 2016 00:16:10 -0700 Subject: [PATCH 1/2] Add a single-line code golf example --- 06-code-golf/tf-06-bm.py | 1 + 1 file changed, 1 insertion(+) create mode 100644 06-code-golf/tf-06-bm.py diff --git a/06-code-golf/tf-06-bm.py b/06-code-golf/tf-06-bm.py new file mode 100644 index 0000000..dc3caae --- /dev/null +++ b/06-code-golf/tf-06-bm.py @@ -0,0 +1 @@ +print (reduce(lambda string, tup: string + tup[0] + ' - ' + str(tup[1]) + '\n', sorted( filter(lambda tup: tup[0] not in open('../stop_words.txt').read().lower().split(','), reduce(lambda word_dict, word: word_dict if (word_dict.__setitem__(word, word_dict.get(word, 0) + 1) if True else None) else word_dict, filter(lambda word: len(word) > 1, (''.join(map(lambda letter: ' ' if ord(letter) not in list(range(ord('a'), ord('z') + 1)) else letter, open('../pride-and-prejudice.txt').read().lower()))).split()), {}).iteritems()), key=lambda tup: tup[1], reverse=True)[0:25], '')) # hole in one? From c94acde9bb38f59d3222430247e24b27a7831556 Mon Sep 17 00:00:00 2001 From: bmistree Date: Fri, 20 May 2016 22:34:44 -0700 Subject: [PATCH 2/2] Take filename argument from program arguments Previously, I had hard-coded this. This commit also makes it so that the script can be executed from any current working directory and (hopefully) makes it platform agnostic by using os.path.join to build the path to the stop words file. --- 06-code-golf/tf-06-bm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/06-code-golf/tf-06-bm.py b/06-code-golf/tf-06-bm.py index dc3caae..2a1fb63 100644 --- a/06-code-golf/tf-06-bm.py +++ b/06-code-golf/tf-06-bm.py @@ -1 +1 @@ -print (reduce(lambda string, tup: string + tup[0] + ' - ' + str(tup[1]) + '\n', sorted( filter(lambda tup: tup[0] not in open('../stop_words.txt').read().lower().split(','), reduce(lambda word_dict, word: word_dict if (word_dict.__setitem__(word, word_dict.get(word, 0) + 1) if True else None) else word_dict, filter(lambda word: len(word) > 1, (''.join(map(lambda letter: ' ' if ord(letter) not in list(range(ord('a'), ord('z') + 1)) else letter, open('../pride-and-prejudice.txt').read().lower()))).split()), {}).iteritems()), key=lambda tup: tup[1], reverse=True)[0:25], '')) # hole in one? +print (reduce(lambda string, tup: string + tup[0] + ' - ' + str(tup[1]) + '\n', sorted( filter(lambda tup: tup[0] not in open(__import__('os').path.join(__import__('os').path.dirname(__file__), '..', 'stop_words.txt')).read().lower().split(','), reduce(lambda word_dict, word: word_dict if (word_dict.__setitem__(word, word_dict.get(word, 0) + 1) if True else None) else word_dict, filter(lambda word: len(word) > 1, (''.join(map(lambda letter: ' ' if ord(letter) not in set(range(ord('a'), ord('z') + 1)) else letter, open(__import__('sys').argv[1]).read().lower()))).split()), {}).iteritems()), key=lambda tup: tup[1], reverse=True)[0:25], '')) # hole in one?