From bc48463d44bc4e113304ff030c172ad858702bbb Mon Sep 17 00:00:00 2001 From: Kyle K Date: Thu, 4 Aug 2022 01:31:23 -0500 Subject: wordcount example --- wordcount.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 wordcount.py 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) -- cgit v1.2.3