aboutsummaryrefslogtreecommitdiff
path: root/challenge-206/roger-bell-west/python/ch-2.py
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-206/roger-bell-west/python/ch-2.py')
-rwxr-xr-xchallenge-206/roger-bell-west/python/ch-2.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/challenge-206/roger-bell-west/python/ch-2.py b/challenge-206/roger-bell-west/python/ch-2.py
new file mode 100755
index 0000000000..bcd87e7cfd
--- /dev/null
+++ b/challenge-206/roger-bell-west/python/ch-2.py
@@ -0,0 +1,31 @@
+#! /usr/bin/python3
+
+import unittest
+from itertools import permutations, combinations
+
+def arraypairing(n):
+ nl = len(n)
+ if nl % 2 == 1:
+ return 0
+ hl = nl // 2
+ out = []
+ for px in combinations(range(nl), hl):
+ pa = [n[i] for i in px]
+ ps = set(px)
+ pb = [n[i] for i in range(nl) if i not in ps]
+ for pp in permutations(pa):
+ s = 0
+ for i in range(hl):
+ s += min(pp[i], pb[i])
+ out.append(s)
+ return max(out)
+
+class TestArraypairing(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(arraypairing([1, 2, 3, 4]), 4, 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(arraypairing([0, 2, 1, 3]), 2, 'example 2')
+
+unittest.main()