From 5cde2691ea8d6dae23f2ce3967480ac9dd10f927 Mon Sep 17 00:00:00 2001 From: Crista Lopes Date: Mon, 21 Oct 2013 17:07:36 -0700 Subject: [PATCH] Added spreadsheet style --- 26-spreadsheet/README.md | 16 ++++++++++++++++ 26-spreadsheet/tf-26.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 26-spreadsheet/README.md create mode 100644 26-spreadsheet/tf-26.py diff --git a/26-spreadsheet/README.md b/26-spreadsheet/README.md new file mode 100644 index 0000000..77f4afc --- /dev/null +++ b/26-spreadsheet/README.md @@ -0,0 +1,16 @@ +Style #26 +============================== + +Constraints: + +- The problem is modeled like a spreadsheet, with columns of data and formulas + +- Some data depends on other data according to formulas. When data + changes, the dependent data also changes automatically. + + +Possible names: + +- Spreadsheet +- Dataflow +- Active data diff --git a/26-spreadsheet/tf-26.py b/26-spreadsheet/tf-26.py new file mode 100644 index 0000000..c068d12 --- /dev/null +++ b/26-spreadsheet/tf-26.py @@ -0,0 +1,38 @@ +import sys, re, itertools, operator + +# +# The columns. Each column comprises of a data element and a formula. +# The first 2 columns are the input data, so no formulas. +# +all_words = [(), None] +stop_words = [(), None] +non_stop_words = [(), lambda w : w if w not in stop_words[0] else ''] +unique_words = [(), lambda word_list: set([w for w in word_list if w != ''])] +counts = [(), lambda w, word_list : word_list.count(w)] +sorted_data = [(), lambda z : sorted(z, key=operator.itemgetter(1), reverse=True)] + +# +# The active procedure over the columns of data. +# Call this everytime the input data changes, or periodically. +# +def update(): + global non_stop_words + global unique_words + global counts + global sorted_data + # Apply the formulas to the 4 last columns + non_stop_words[0] = map(non_stop_words[1], all_words[0]) + unique_words[0] = unique_words[1](non_stop_words[0]) + counts[0] = map(counts[1], unique_words[0], itertools.repeat(non_stop_words[0], len(unique_words[0]))) + sorted_data[0] = sorted_data[1](zip(list(unique_words[0]), counts[0])) +# +# Load the fixed data into the first 2 columns +# +all_words[0] = re.findall('[a-z]{2,}', open(sys.argv[1]).read().lower()) +stop_words[0] = set(open('../stop_words.txt').read().split(',')) +# Update the columns with formulas +update() + +# Display +for (w, c) in sorted_data[0][:25]: + print w, '-', c