aboutsummaryrefslogtreecommitdiff
path: root/challenge-270/roger-bell-west/python/ch-2.py
diff options
context:
space:
mode:
authorRoger Bell_West <roger@firedrake.org>2024-05-21 13:24:25 +0100
committerRoger Bell_West <roger@firedrake.org>2024-05-21 13:24:25 +0100
commit934105849615c077f262a0dd4bbf8a5fa04e0307 (patch)
tree22e6fa3931b2fe63b2a9e12bc0d64dd6ed63e7e4 /challenge-270/roger-bell-west/python/ch-2.py
parented462bf99ed6fda013ab14d58855951ef13b05fa (diff)
downloadperlweeklychallenge-club-934105849615c077f262a0dd4bbf8a5fa04e0307.tar.gz
perlweeklychallenge-club-934105849615c077f262a0dd4bbf8a5fa04e0307.tar.bz2
perlweeklychallenge-club-934105849615c077f262a0dd4bbf8a5fa04e0307.zip
RogerBW solutions for challenge no. 270
Diffstat (limited to 'challenge-270/roger-bell-west/python/ch-2.py')
-rwxr-xr-xchallenge-270/roger-bell-west/python/ch-2.py41
1 files changed, 41 insertions, 0 deletions
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()