From c9d89cb678eae8e104fbdd97c03fe4579ac40461 Mon Sep 17 00:00:00 2001 From: Richard Kojedzinszky Date: Fri, 26 Sep 2014 09:53:14 +0200 Subject: [PATCH] python version produces the same output --- uwc.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/uwc.py b/uwc.py index 5367cba..8a79795 100644 --- a/uwc.py +++ b/uwc.py @@ -9,7 +9,17 @@ for line in sys.stdin: for w in word_re.findall(line): counts[w] = counts.get(w, 0) + 1 +def sorter(a, b): + if counts[a] != counts[b]: + return counts[a] - counts[b] -words = sorted(counts, key=lambda x: counts[x]) + if a < b: + return -1 + if a > b: + return 1 + + return 0 + +words = sorted(counts, cmp=sorter) for w in words: - print w + print w, counts[w] -- 2.1.4