diff options
-rw-r--r-- | wordcount.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/wordcount.py b/wordcount.py new file mode 100644 index 0000000..11a34fd --- /dev/null +++ b/wordcount.py @@ -0,0 +1,19 @@ +""" +problem: count ocurrences of words +solution: tr ' ' '\n' | sort | uniq -c +e.g.: echo "When the going gets tough, the tough get going" | tr -s ' ' '\n' | sort | uniq -c | sort -bnr + +heuristic: use a hashtable, a dictionary in python is close enough and will suffice +""" + +import sys + +counts = {} +for line in sys.stdin: + words = line.lower().split() + for word in words: + counts[word] = counts.get(word, 0) + 1 + +tupleList = sorted(counts.items(), key=lambda tup: tup[1], reverse=True) +for word, count in tupleList: + print(word, count) |