From 1d23f91d86e7ebb5291c17fc99ec0fc2ca48b64a Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 17 Oct 2022 11:05:10 +0100 Subject: Solutions for challenge #187 --- challenge-187/roger-bell-west/python/ch-1.py | 43 ++++++++++++++++++++++++++++ challenge-187/roger-bell-west/python/ch-2.py | 40 ++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100755 challenge-187/roger-bell-west/python/ch-1.py create mode 100755 challenge-187/roger-bell-west/python/ch-2.py (limited to 'challenge-187/roger-bell-west/python') diff --git a/challenge-187/roger-bell-west/python/ch-1.py b/challenge-187/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..b7be1cd5c4 --- /dev/null +++ b/challenge-187/roger-bell-west/python/ch-1.py @@ -0,0 +1,43 @@ +#! /usr/bin/python3 + +import unittest + +from datetime import date, timedelta +import re + +def s2date(ds): + dm = re.findall(r"\d+", ds) + if len(dm) == 2: + return date(2022, int(dm[1]), int(dm[0])) + +def daystogether(a, b): + starts = sorted([s2date(a[0]), s2date(b[0])]) + ends = sorted([s2date(a[1]), s2date(b[1])]) + if ends[0] >= starts[1]: + return (ends[0]-starts[1]).days+1 + else: + return 0 + +class TestDaystogether(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(daystogether(["12-01", "20-01"], ["15-01", "18-01"]), + 4, + 'example 1') + + def test_ex2(self): + self.assertEqual(daystogether(["02-03", "12-03"], ["13-03", "14-03"]), + 0, + 'example 2') + + def test_ex3(self): + self.assertEqual(daystogether(["02-03", "12-03"], ["11-03", "15-03"]), + 2, + 'example 3') + + def test_ex4(self): + self.assertEqual(daystogether(["30-03", "05-04"], ["28-03", "02-04"]), + 4, + 'example 4') + +unittest.main() diff --git a/challenge-187/roger-bell-west/python/ch-2.py b/challenge-187/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..0686ce96cd --- /dev/null +++ b/challenge-187/roger-bell-west/python/ch-2.py @@ -0,0 +1,40 @@ +#! /usr/bin/python3 + +import unittest + +from itertools import combinations + +def magicaltriplets(a): + out = [] + mv = 0 + for b in combinations(a, 3): + if b[0] + b[1] > b[2] and b[1] + b[2] > b[0] and b[0] + b[2] > b[1]: + v = sum(b) + if v > mv: + mv = v + out = b + return list(reversed(sorted(out))) + +class TestMagicaltriplets(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(magicaltriplets([1, 2, 3, 2]), + [3, 2, 2], + 'example 1') + + def test_ex2(self): + self.assertEqual(magicaltriplets([1, 3, 2]), + [], + 'example 2') + + def test_ex3(self): + self.assertEqual(magicaltriplets([1, 1, 2, 3]), + [], + 'example 3') + + def test_ex4(self): + self.assertEqual(magicaltriplets([2, 4, 3]), + [4, 3, 2], + 'example 4') + +unittest.main() -- cgit