diff options
| author | Conor Hoekstra <codereport@outlook.com> | 2024-09-09 16:34:38 -0400 |
|---|---|---|
| committer | Conor Hoekstra <codereport@outlook.com> | 2024-09-09 16:34:38 -0400 |
| commit | 2f3cf4f7cfa808d89e183b77f61dac80db4ab751 (patch) | |
| tree | 9dcefb9d7860406bce98f0666cd99d7b608dd758 /challenge-285/sgreen/python | |
| parent | 156e3186ef5b8f929e3589f8469463132e0fa996 (diff) | |
| parent | 148ad068f27ef5bbdcac38b14685b2273b0d02ec (diff) | |
| download | perlweeklychallenge-club-2f3cf4f7cfa808d89e183b77f61dac80db4ab751.tar.gz perlweeklychallenge-club-2f3cf4f7cfa808d89e183b77f61dac80db4ab751.tar.bz2 perlweeklychallenge-club-2f3cf4f7cfa808d89e183b77f61dac80db4ab751.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-285/sgreen/python')
| -rwxr-xr-x | challenge-285/sgreen/python/ch-1.py | 35 | ||||
| -rwxr-xr-x | challenge-285/sgreen/python/ch-2.py | 30 | ||||
| -rwxr-xr-x | challenge-285/sgreen/python/test.py | 22 |
3 files changed, 87 insertions, 0 deletions
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() |
