aboutsummaryrefslogtreecommitdiff
path: root/challenge-191/roger-bell-west/python/ch-2.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-191/roger-bell-west/python/ch-2.py')
-rwxr-xr-xchallenge-191/roger-bell-west/python/ch-2.py43
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-191/roger-bell-west/python/ch-2.py b/challenge-191/roger-bell-west/python/ch-2.py
new file mode 100755
index 0000000000..a06335557f
--- /dev/null
+++ b/challenge-191/roger-bell-west/python/ch-2.py
@@ -0,0 +1,43 @@
+#! /usr/bin/python3
+
+import unittest
+
+def cutelist(n):
+ tab = [[False]]
+ for x in range(1, n+1):
+ tab.append([False] * (n+1))
+ for x in range(1, n+1):
+ for y in range(1, x+1):
+ if x % y != 0 and y % x != 0:
+ tab[x][y] = True
+ tab[y][x] = True
+ count = 0
+ stackl = [[]]
+ stackc = [[x for x in range(1,n+1)]]
+ while len(stackl) > 0:
+ l = stackl.pop()
+ c = stackc.pop()
+ if len(c) == 0 and len(l) == n:
+ count += 1
+ else:
+ place = len(l) + 1
+ for candidate in c:
+ if not tab[place][candidate]:
+ q = l.copy()
+ q.append(candidate)
+ stackl.append(q)
+ stackc.append([i for i in c if i != candidate])
+ return count
+
+class TestCutelist(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(cutelist(2), 2, 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(cutelist(10), 700, 'example 2')
+
+ def test_ex3(self):
+ self.assertEqual(cutelist(15), 24679, 'example 3')
+
+unittest.main()