aboutsummaryrefslogtreecommitdiff
path: root/challenge-219/roger-bell-west/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-219/roger-bell-west/python')
-rwxr-xr-xchallenge-219/roger-bell-west/python/ch-1.py18
-rwxr-xr-xchallenge-219/roger-bell-west/python/ch-2.py36
2 files changed, 54 insertions, 0 deletions
diff --git a/challenge-219/roger-bell-west/python/ch-1.py b/challenge-219/roger-bell-west/python/ch-1.py
new file mode 100755
index 0000000000..b6737234e7
--- /dev/null
+++ b/challenge-219/roger-bell-west/python/ch-1.py
@@ -0,0 +1,18 @@
+#! /usr/bin/python3
+
+def sortedsquares(lst):
+ q = [i * i for i in lst]
+ q.sort()
+ return q
+
+import unittest
+
+class TestSortedsquares(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(sortedsquares([-2, -1, 0, 3, 4]), [0, 1, 4, 9, 16], 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(sortedsquares([5, -4, -1, 3, 6]), [1, 9, 16, 25, 36], 'example 2')
+
+unittest.main()
diff --git a/challenge-219/roger-bell-west/python/ch-2.py b/challenge-219/roger-bell-west/python/ch-2.py
new file mode 100755
index 0000000000..d747e7ad69
--- /dev/null
+++ b/challenge-219/roger-bell-west/python/ch-2.py
@@ -0,0 +1,36 @@
+#! /usr/bin/python3
+
+from collections import deque
+
+def travelexpenditure(costs, days0):
+ days = sorted(days0)
+ validities = [1, 7, 30]
+ stack = deque()
+ stack.append([0, days])
+ cheapest = len(days) * costs[0]
+ while len(stack) > 0:
+ c = stack.popleft()
+ if len(c[1]) == 0:
+ if c[0] < cheapest:
+ cheapest = c[0]
+ else:
+ if c[0] >= cheapest:
+ continue
+ start = c[1][0]
+ for i in range(3):
+ end = start + validities[i] - 1
+ dtd = list(filter(lambda x: x > end, c[1]))
+ stack.append([c[0] + costs[i], dtd])
+ return cheapest
+
+import unittest
+
+class TestTravelexpenditure(unittest.TestCase):
+
+ def test_ex1(self):
+ self.assertEqual(travelexpenditure([2, 7, 25], [1, 5, 6, 7, 9, 15]), 11, 'example 1')
+
+ def test_ex2(self):
+ self.assertEqual(travelexpenditure([2, 7, 25], [1, 2, 3, 5, 7, 10, 11, 12, 14, 20, 30, 31]), 20, 'example 2')
+
+unittest.main()