diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2023-12-27 14:57:27 -0600 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2023-12-27 14:57:27 -0600 |
| commit | f9c88dbbb1e13bfb63f40d0dd3b8630850655d7b (patch) | |
| tree | cb922fb2f44a7ec7bd10cd13b3db9eb2d4fd14b5 /challenge-249/roger-bell-west/python | |
| parent | 23f378718ed93b9b8d9e51fe18f07a55f61dc9c1 (diff) | |
| parent | 4ece120b2f4c7b25b00eaad66524fb5d99fa58c3 (diff) | |
| download | perlweeklychallenge-club-f9c88dbbb1e13bfb63f40d0dd3b8630850655d7b.tar.gz perlweeklychallenge-club-f9c88dbbb1e13bfb63f40d0dd3b8630850655d7b.tar.bz2 perlweeklychallenge-club-f9c88dbbb1e13bfb63f40d0dd3b8630850655d7b.zip | |
Merge branch 'master' of github.com:manwar/perlweeklychallenge-club into challenges
Diffstat (limited to 'challenge-249/roger-bell-west/python')
| -rwxr-xr-x | challenge-249/roger-bell-west/python/ch-1.py | 25 | ||||
| -rwxr-xr-x | challenge-249/roger-bell-west/python/ch-2.py | 35 |
2 files changed, 60 insertions, 0 deletions
diff --git a/challenge-249/roger-bell-west/python/ch-1.py b/challenge-249/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..e086773409 --- /dev/null +++ b/challenge-249/roger-bell-west/python/ch-1.py @@ -0,0 +1,25 @@ +#! /usr/bin/python3 + +def shortestdistance(a0): + if len(a0) % 2 != 0: + return [] + a = a0 + a.sort() + out = [] + for i in range(0, len(a), 2): + if a[i] != a[i+1]: + return [] + out.append([a[i], a[i]]) + return out + +import unittest + +class TestShortestdistance(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(shortestdistance([3, 2, 3, 2, 2, 2]), [[2, 2], [2, 2], [3, 3]], 'example 1') + + def test_ex2(self): + self.assertEqual(shortestdistance([1, 2, 3, 4]), [], 'example 2') + +unittest.main() diff --git a/challenge-249/roger-bell-west/python/ch-2.py b/challenge-249/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..a242036ab7 --- /dev/null +++ b/challenge-249/roger-bell-west/python/ch-2.py @@ -0,0 +1,35 @@ +#! /usr/bin/python3 + +def distringmatch(a): + v = 1 << (len(a) - 1) + wv = v << 1 + out = [wv] + for c in a: + if c == "I": + wv += v + else: + wv -= v + v >>= 1 + out.append(wv) + c = dict() + q = out.copy() + q.sort() + for i, v in enumerate(q): + c[v] = i + return [c[v] for v in out] + + +import unittest + +class TestDistringmatch(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(distringmatch("IDID"), [0, 4, 1, 3, 2], 'example 1') + + def test_ex2(self): + self.assertEqual(distringmatch("III"), [0, 1, 2, 3], 'example 2') + + def test_ex3(self): + self.assertEqual(distringmatch("DDI"), [3, 2, 0, 1], 'example 3') + +unittest.main() |
