aboutsummaryrefslogtreecommitdiff
path: root/challenge-270/roger-bell-west/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-270/roger-bell-west/python')
-rwxr-xr-xchallenge-270/roger-bell-west/python/ch-1.py37
-rwxr-xr-xchallenge-270/roger-bell-west/python/ch-2.py41
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-270/roger-bell-west/python/ch-1.py b/challenge-270/roger-bell-west/python/ch-1.py
new file mode 100755
index 0000000000..56c82863e0
--- /dev/null
+++ b/challenge-270/roger-bell-west/python/ch-1.py
@@ -0,0 +1,37 @@
+#! /usr/bin/python3
+
+def validator(a0):
+ a = a0.copy()
+ a.sort()
+ l = len(a)
+ if a[0] == 0 and a[l - 2] == 0 and a[l - 1] == 1:
+ return a0.index(1)
+ return -1
+
+def specialpositions(a):
+ vr = set()
+ xs = set()
+ for y, row in enumerate(a):
+ x = validator(row)
+ if (x > -1):
+ vr.add((y, x))
+ xs.add(x)
+ xd = set()
+ for x in xs:
+ c = [r[x] for r in a]
+ if validator(c) == -1:
+ xd.add(x)
+ xs -= xd
+ return len([x for (y, x) in vr if x in xs])
+
+import unittest
+
+class TestSpecialpositions(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(specialpositions([[1, 0, 0], [0, 0, 1], [1, 0, 0]]), 1, 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(specialpositions([[1, 0, 0], [0, 1, 0], [0, 0, 1]]), 3, 'example 2')
+
+unittest.main()
diff --git a/challenge-270/roger-bell-west/python/ch-2.py b/challenge-270/roger-bell-west/python/ch-2.py
new file mode 100755
index 0000000000..daeb95ee64
--- /dev/null
+++ b/challenge-270/roger-bell-west/python/ch-2.py
@@ -0,0 +1,41 @@
+#! /usr/bin/python3
+
+from collections import deque
+
+def equalizearray(a0, x, y):
+ a = a0.copy()
+ a.sort()
+ limit = a[-1]
+ queue = deque()
+ queue.append((a, 0))
+ mc = -1
+ while len(queue) > 0:
+ op = queue.popleft()
+ if mc == -1 or mc > op[1]:
+ if op[0][0] == op[0][-1]:
+ mc = op[1]
+ else:
+ p = op[0].copy()
+ p[0] += 1
+ if p[0] <= limit:
+ p.sort()
+ queue.append((p, op[1] + x))
+ q = op[0].copy()
+ q[0] += 1;
+ q[1] += 1;
+ if q[1] <= limit:
+ q.sort()
+ queue.append((q, op[1] + y))
+ return mc
+
+import unittest
+
+class TestEqualizearray(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(equalizearray([4, 1], 3, 2), 9, 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(equalizearray([2, 3, 3, 3, 5], 2, 1), 6, 'example 2')
+
+unittest.main()