diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2024-09-18 19:56:42 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2024-09-18 19:56:42 -0400 |
| commit | f86f5e2fec16020c1d86f9028fb0f61cfeac106e (patch) | |
| tree | 0fd388a696b51ffde5a7bfe8519a74e1caf42461 /challenge-285/lubos-kolouch/python/ch-2.py | |
| parent | ff8719c86653d5ad3121955e9494a0010527c2b9 (diff) | |
| parent | 0052ec63ca70eaa6d9ffb1926c294dbfd85f8c05 (diff) | |
| download | perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.tar.gz perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.tar.bz2 perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-285/lubos-kolouch/python/ch-2.py')
| -rw-r--r-- | challenge-285/lubos-kolouch/python/ch-2.py | 56 |
1 files changed, 56 insertions, 0 deletions
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() |
