summaryrefslogtreecommitdiffstats
path: root/wordcount.py
blob: 11a34fd734d16bec00cd8b11dfbd3342d66cec7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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)