""" 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)