aboutsummaryrefslogtreecommitdiff
path: root/challenge-285/lubos-kolouch/python/ch-2.py
blob: 60182732c2ccd8c3369c015b30a293969077a6e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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()