From 293c72bd0be6f09dba088d564d92d6820357bb3e Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Tue, 6 Oct 2020 21:08:46 -0400 Subject: python solutions to challenge 81 --- challenge-081/walt-mankowski/python/ch-2.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 challenge-081/walt-mankowski/python/ch-2.py (limited to 'challenge-081/walt-mankowski/python/ch-2.py') 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..2b6c400b45 --- /dev/null +++ b/challenge-081/walt-mankowski/python/ch-2.py @@ -0,0 +1,29 @@ +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) + if line == '': + continue + 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() + -- cgit From 132c71e855a2ba84207bed6ee1e531e6f66f9e6f Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Sat, 10 Oct 2020 19:04:15 -0400 Subject: use split() instead of split('') --- challenge-081/walt-mankowski/python/ch-2.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'challenge-081/walt-mankowski/python/ch-2.py') diff --git a/challenge-081/walt-mankowski/python/ch-2.py b/challenge-081/walt-mankowski/python/ch-2.py index 2b6c400b45..b348f03819 100644 --- a/challenge-081/walt-mankowski/python/ch-2.py +++ b/challenge-081/walt-mankowski/python/ch-2.py @@ -8,9 +8,7 @@ with open(fname) as f: for line in f: line = line.rstrip() line = re.sub('--', ' ', line) - if line == '': - continue - toks = line.split(' ') + toks = line.split() for tok in toks: tok = re.sub('[."(),]', '', tok); tok = re.sub("'s$", '', tok) -- cgit