From 858a5a78afbe2f51d389af1d6f4dd5d4e28d9710 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Tue, 26 Dec 2023 19:56:14 +0000 Subject: RogerBW solutions for challenge no. 249 --- challenge-249/roger-bell-west/python/ch-2.py | 35 ++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 challenge-249/roger-bell-west/python/ch-2.py (limited to 'challenge-249/roger-bell-west/python/ch-2.py') 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() -- cgit