aboutsummaryrefslogtreecommitdiff
path: root/challenge-249/roger-bell-west/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-249/roger-bell-west/python')
-rwxr-xr-xchallenge-249/roger-bell-west/python/ch-1.py25
-rwxr-xr-xchallenge-249/roger-bell-west/python/ch-2.py35
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()