From 34ea064000db8a411364e90df7b72c1d943d3888 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 8 Sep 2024 22:35:25 +1000 Subject: sgreen solutions to challenge 285 --- challenge-285/sgreen/python/ch-1.py | 35 +++++++++++++++++++++++++++++++++++ challenge-285/sgreen/python/ch-2.py | 30 ++++++++++++++++++++++++++++++ challenge-285/sgreen/python/test.py | 22 ++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100755 challenge-285/sgreen/python/ch-1.py create mode 100755 challenge-285/sgreen/python/ch-2.py create mode 100755 challenge-285/sgreen/python/test.py (limited to 'challenge-285/sgreen/python') diff --git a/challenge-285/sgreen/python/ch-1.py b/challenge-285/sgreen/python/ch-1.py new file mode 100755 index 0000000000..f0cbf0030e --- /dev/null +++ b/challenge-285/sgreen/python/ch-1.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +import sys + + +def no_connection(routes: list) -> str: + # Calculate a list of origins (first value) and destinations (second value) + origins = [v[0] for v in routes] + destinations = [v[1] for v in routes] + + # Find route destinations that aren't also an origin + dead_ends = [d for d in destinations if d not in origins] + + if len(dead_ends) > 1: + raise ValueError( + 'There are multiple routes with no outgoing connection') + + if len(dead_ends) == 0: + raise ValueError('All routes have an outgoing connection') + + return dead_ends[0] + + +def main(): + # Convert input into pairs. The first value is the start of route. + # The second value is the destination + routes = [] + for i in range(1, len(sys.argv), 2): + routes.append([sys.argv[i], sys.argv[i+1]]) + result = no_connection(routes) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-285/sgreen/python/ch-2.py b/challenge-285/sgreen/python/ch-2.py new file mode 100755 index 0000000000..6ea25dabac --- /dev/null +++ b/challenge-285/sgreen/python/ch-2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 + +import sys + + +def making_change(remaining: int, last_coin: int | None = None) -> int: + combinations = 0 + + for coin in [1, 5, 10, 25, 50]: + if last_coin and last_coin < coin: + # We can't use a larger coin that the last one + continue + if coin == remaining: + # We have found a solution + combinations += 1 + if coin < remaining: + # Call the function again, taking away the value of the coin + combinations += making_change(remaining-coin, coin) + + # This value is returned upstream as we go + return combinations + + +def main(): + result = making_change(int(sys.argv[1])) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-285/sgreen/python/test.py b/challenge-285/sgreen/python/test.py new file mode 100755 index 0000000000..5ac058a985 --- /dev/null +++ b/challenge-285/sgreen/python/test.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python3 + +import unittest +ch_1 = __import__('ch-1') +ch_2 = __import__('ch-2') + + +class TestClass(unittest.TestCase): + def test_ch_1(self): + self.assertEqual( + ch_1.no_connection([['B', 'C'], ['D','B'], ['C','A']]), 'A' + ) + self.assertEqual(ch_1.no_connection([['A', 'Z']]), 'Z') + + def test_ch_2(self): + self.assertEqual(ch_2.making_change(9), 2) + self.assertEqual(ch_2.making_change(15), 6) + self.assertEqual(ch_2.making_change(100), 292) + + +if __name__ == '__main__': + unittest.main() -- cgit