python version produces the same output
authorRichard Kojedzinszky <krichy@tvnetwork.hu>
Fri, 26 Sep 2014 07:53:14 +0000 (09:53 +0200)
committerRichard Kojedzinszky <krichy@tvnetwork.hu>
Fri, 26 Sep 2014 07:53:14 +0000 (09:53 +0200)
uwc.py

diff --git a/uwc.py b/uwc.py
index 5367cba..8a79795 100644 (file)
--- 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]