diff options
Diffstat (limited to 'challenge-081/roger-bell-west/python')
| -rwxr-xr-x | challenge-081/roger-bell-west/python/ch-1.py | 48 | ||||
| -rwxr-xr-x | challenge-081/roger-bell-west/python/ch-2.py | 23 |
2 files changed, 71 insertions, 0 deletions
diff --git a/challenge-081/roger-bell-west/python/ch-1.py b/challenge-081/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..42f02ed19b --- /dev/null +++ b/challenge-081/roger-bell-west/python/ch-1.py @@ -0,0 +1,48 @@ +#! /usr/bin/python3 + +import unittest +from math import sqrt + +def cbs(*strs): + bss=map(bs,strs) + r=set() + f=False + for bsa in bss: + if (f): + s=set(bsa) + r=r & s + else: + r=set(bsa) + f=True + return sorted(r) + +def bs(str): + sl=len(str) + f=dict() + for n in range(1,int(sqrt(sl))+1): + p=sl/n + if (p == int(p)): + f[n]=p + f[p]=n + out=list() + for l in sorted(f.keys()): + q=str[0:int(l)] + if (q * int(f[l]) == str): + out.append(q) + return out + +class TestCbs(unittest.TestCase): + + def test_bs1(self): + self.assertEqual(bs('abcdabcd'),['abcd','abcdabcd'],'bs-only 1') + + def test_bs2(self): + self.assertEqual(bs('aaa'),['a','aaa'],'bs-only 2') + + def test_ex1(self): + self.assertEqual(cbs('abcdabcd','abcdabcdabcdabcd'),['abcd','abcdabcd'],'example 1') + + def test_ex2(self): + self.assertEqual(cbs('aaa','aa'),['a',],'example 2') + +unittest.main() diff --git a/challenge-081/roger-bell-west/python/ch-2.py b/challenge-081/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..eca8fb3cc3 --- /dev/null +++ b/challenge-081/roger-bell-west/python/ch-2.py @@ -0,0 +1,23 @@ +#! /usr/bin/python3 + +import fileinput +from itertools import chain + +c=dict() + +for line in fileinput.input(): + line=line.rstrip("\r\n") + for rep in ('--',"'s",'.','"','(',')',','): + line=line.replace(rep,' ') + for word in line.split(): + c.setdefault(word,0) + c[word] += 1 + +f=dict() +for w in sorted(c.keys()): + f.setdefault(c[w],list()) + f[c[w]].append(w) + +for n in sorted(f.keys()): + print(' '.join((list(str(n)) + f[n]))) + print('') |
