aboutsummaryrefslogtreecommitdiff
path: root/challenge-081/walt-mankowski/python/ch-2.py
diff options
context:
space:
mode:
authorMyoungjin JEON <jeongoon@gmail.com>2020-10-15 00:04:11 +1100
committerMyoungjin JEON <jeongoon@gmail.com>2020-10-15 00:04:11 +1100
commitdd71532a73981ec613c8c61d99fa4bbd3f3fd79c (patch)
tree70b1b5c1613f51461bb27f6b4aa49ba8b5efa78e /challenge-081/walt-mankowski/python/ch-2.py
parent99053d27c19ab6baee10484e8de730527333ddde (diff)
parenta86e289ff2bb02e7c579be9175c92f2667168a28 (diff)
downloadperlweeklychallenge-club-dd71532a73981ec613c8c61d99fa4bbd3f3fd79c.tar.gz
perlweeklychallenge-club-dd71532a73981ec613c8c61d99fa4bbd3f3fd79c.tar.bz2
perlweeklychallenge-club-dd71532a73981ec613c8c61d99fa4bbd3f3fd79c.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-081/walt-mankowski/python/ch-2.py')
-rw-r--r--challenge-081/walt-mankowski/python/ch-2.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/challenge-081/walt-mankowski/python/ch-2.py b/challenge-081/walt-mankowski/python/ch-2.py
new file mode 100644
index 0000000000..b348f03819
--- /dev/null
+++ b/challenge-081/walt-mankowski/python/ch-2.py
@@ -0,0 +1,27 @@
+from sys import argv
+from collections import defaultdict
+import re
+
+d = defaultdict(int)
+fname = argv[1]
+with open(fname) as f:
+ for line in f:
+ line = line.rstrip()
+ line = re.sub('--', ' ', line)
+ toks = line.split()
+ for tok in toks:
+ tok = re.sub('[."(),]', '', tok);
+ tok = re.sub("'s$", '', tok)
+ d[tok] += 1
+
+last = 0
+for k in sorted(d, key=lambda k:(d[k], k)):
+ if d[k] != last:
+ if last != 0:
+ print()
+ print(d[k], end='')
+ last = d[k]
+ print(f" {k}", end = '')
+
+print()
+