diff options
Diffstat (limited to 'challenge-285/lubos-kolouch/python')
| -rw-r--r-- | challenge-285/lubos-kolouch/python/ch-1.py | 52 | ||||
| -rw-r--r-- | challenge-285/lubos-kolouch/python/ch-2.py | 56 |
2 files changed, 108 insertions, 0 deletions
diff --git a/challenge-285/lubos-kolouch/python/ch-1.py b/challenge-285/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..be24d53a49 --- /dev/null +++ b/challenge-285/lubos-kolouch/python/ch-1.py @@ -0,0 +1,52 @@ +from typing import List +import unittest + + +def find_destination(routes: List[List[str]]) -> str: + """ + Finds the destination with no further outgoing connection from a given list of routes. + + Each route is represented as a list containing a source and a destination city. + + Args: + routes (List[List[str]]): A list of routes. + + Returns: + str: The destination city with no outgoing connections. + + """ + sources = set() + destinations = set() + + for route in routes: + source, destination = route + sources.add(source) + destinations.add(destination) + + no_outgoing = destinations - sources + + if no_outgoing: + # Assuming there is only one such city + return no_outgoing.pop() + else: + return None # In case there is no such city + + +# Unit Tests +class TestNoConnection(unittest.TestCase): + + def test_example1(self): + routes = [["B", "C"], ["D", "B"], ["C", "A"]] + self.assertEqual(find_destination(routes), "A", 'Example 1') + + def test_example2(self): + routes = [["A", "Z"]] + self.assertEqual(find_destination(routes), "Z", 'Example 2') + + def test_no_destination(self): + routes = [["A", "B"], ["B", "A"]] + self.assertIsNone(find_destination(routes), 'No unique destination') + + +if __name__ == "__main__": + unittest.main() diff --git a/challenge-285/lubos-kolouch/python/ch-2.py b/challenge-285/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..60182732c2 --- /dev/null +++ b/challenge-285/lubos-kolouch/python/ch-2.py @@ -0,0 +1,56 @@ +from typing import List +import unittest + + +def making_change(amount: int) -> int: + """ + Compute the number of ways to make change for the given amount in cents using US coins. + + Coins available: + - Penny (P): 1 cent + - Nickel (N): 5 cents + - Dime (D): 10 cents + - Quarter (Q): 25 cents + - Half-dollar (HD): 50 cents + + Order of coin selection does not matter. + + Args: + amount (int): The amount in cents (non-negative integer). + + Returns: + int: The number of distinct ways to make change. + """ + coins = [1, 5, 10, 25, 50] + dp = [0] * (amount + 1) + dp[0] = 1 # There is one way to make 0 cents + + for coin in coins: + for i in range(coin, amount + 1): + dp[i] += dp[i - coin] + + return dp[amount] + + +# Unit Tests +class TestMakingChange(unittest.TestCase): + + def test_example1(self): + self.assertEqual(making_change(9), 2, 'Example 1') + + def test_example2(self): + self.assertEqual(making_change(15), 6, 'Example 2') + + def test_example3(self): + self.assertEqual(making_change(100), 292, 'Example 3') + + def test_zero_amount(self): + self.assertEqual(making_change(0), 1, 'Zero amount') + + def test_negative_amount(self): + with self.assertRaises(IndexError): + making_change(-1) + + +if __name__ == "__main__": + unittest.main() |
