diff options
| author | Roger Bell_West <roger@firedrake.org> | 2023-05-29 10:00:17 +0100 |
|---|---|---|
| committer | Roger Bell_West <roger@firedrake.org> | 2023-05-29 10:00:17 +0100 |
| commit | 66cfec5ab3b80c0bf4f3e3229f3eecf88840734d (patch) | |
| tree | f8483e0acc157ffbd96bc29d7afeb5bd7bc8f979 /challenge-219/roger-bell-west/python | |
| parent | 979144e452a65703e7845a166d7c94ba6f89e37f (diff) | |
| download | perlweeklychallenge-club-66cfec5ab3b80c0bf4f3e3229f3eecf88840734d.tar.gz perlweeklychallenge-club-66cfec5ab3b80c0bf4f3e3229f3eecf88840734d.tar.bz2 perlweeklychallenge-club-66cfec5ab3b80c0bf4f3e3229f3eecf88840734d.zip | |
RogerBW solutions for challenge no. 219
Diffstat (limited to 'challenge-219/roger-bell-west/python')
| -rwxr-xr-x | challenge-219/roger-bell-west/python/ch-1.py | 18 | ||||
| -rwxr-xr-x | challenge-219/roger-bell-west/python/ch-2.py | 36 |
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() |
