aboutsummaryrefslogtreecommitdiff
path: root/challenge-211/roger-bell-west/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-211/roger-bell-west/python')
-rwxr-xr-xchallenge-211/roger-bell-west/python/ch-1.py34
-rwxr-xr-xchallenge-211/roger-bell-west/python/ch-2.py32
2 files changed, 66 insertions, 0 deletions
diff --git a/challenge-211/roger-bell-west/python/ch-1.py b/challenge-211/roger-bell-west/python/ch-1.py
new file mode 100755
index 0000000000..a1c6e0e380
--- /dev/null
+++ b/challenge-211/roger-bell-west/python/ch-1.py
@@ -0,0 +1,34 @@
+#! /usr/bin/python3
+
+import unittest
+
+def toeplitzmatrix(a):
+ ym = len(a) - 1
+ xm = len(a[0]) - 1
+ toeplitz = True
+ for xb in range(1 - xm, ym):
+ init = True
+ tv = 0
+ for x in range(xb, xb + xm + 1):
+ if x >= 0 and x <= xm:
+ y = x - xb
+ if y >= 0 and y <= ym:
+ if init:
+ init = False
+ tv = a[y][x]
+ elif a[y][x] != tv:
+ toeplitz = False
+ break
+ if not toeplitz:
+ break
+ return toeplitz
+
+class TestToeplitzmatrix(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(toeplitzmatrix([[4, 3, 2, 1], [5, 4, 3, 2], [6, 5, 4, 3]]), True, 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(toeplitzmatrix([[1, 2, 3], [3, 2, 1]]), False, 'example 2')
+
+unittest.main()
diff --git a/challenge-211/roger-bell-west/python/ch-2.py b/challenge-211/roger-bell-west/python/ch-2.py
new file mode 100755
index 0000000000..7a4f8fcc6d
--- /dev/null
+++ b/challenge-211/roger-bell-west/python/ch-2.py
@@ -0,0 +1,32 @@
+#! /usr/bin/python3
+
+import unittest
+from itertools import combinations
+
+def splitsameaverage(a):
+ ss = sum(a)
+ ml = len(a)
+ mx = int(ml / 2)
+ ssa = False
+ for n in range(1, mx + 1):
+ for c in combinations(a, n):
+ ca = sum(c)
+ if (float(ca) / float(n) == float(ss - ca) / float(ml - n)):
+ ssa = True
+ break
+ if ssa:
+ break
+ return ssa
+
+class TestSplitsameaverage(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(splitsameaverage([1, 2, 3, 4, 5, 6, 7, 8]), True, 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(splitsameaverage([1, 3]), False, 'example 2')
+
+ def test_ex3(self):
+ self.assertEqual(splitsameaverage([1, 2, 3]), True, 'example 3')
+
+unittest.main()