|
| 1 | +"""Coin sums |
| 2 | +
|
| 3 | +In England the currency is made up of pound, £, and pence, p, and there are |
| 4 | +eight coins in general circulation: |
| 5 | +
|
| 6 | +1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p). |
| 7 | +It is possible to make £2 in the following way: |
| 8 | +
|
| 9 | +1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p |
| 10 | +How many different ways can £2 be made using any number of coins? |
| 11 | +
|
| 12 | +Hint: |
| 13 | + > There are 100 pence in a pound (£1 = 100p) |
| 14 | + > There are coins(in pence) are available: 1, 2, 5, 10, 20, 50, 100 and 200. |
| 15 | + > how many different ways you can combine these values to create 200 pence. |
| 16 | +
|
| 17 | +Example: |
| 18 | + to make 6p there are 5 ways |
| 19 | + 1,1,1,1,1,1 |
| 20 | + 1,1,1,1,2 |
| 21 | + 1,1,2,2 |
| 22 | + 2,2,2 |
| 23 | + 1,5 |
| 24 | + to make 5p there are 4 ways |
| 25 | + 1,1,1,1,1 |
| 26 | + 1,1,1,2 |
| 27 | + 1,2,2 |
| 28 | + 5 |
| 29 | +""" |
| 30 | + |
| 31 | + |
| 32 | +def solution(pence: int) -> int: |
| 33 | + """Returns the number of different ways to make X pence using any number of coins. |
| 34 | + The solution is based on dynamic programming paradigm in a bottom-up fashion. |
| 35 | +
|
| 36 | + >>> solution(500) |
| 37 | + 6295434 |
| 38 | + >>> solution(200) |
| 39 | + 73682 |
| 40 | + >>> solution(50) |
| 41 | + 451 |
| 42 | + >>> solution(10) |
| 43 | + 11 |
| 44 | + """ |
| 45 | + coins = [1, 2, 5, 10, 20, 50, 100, 200] |
| 46 | + number_of_ways = [0] * (pence + 1) |
| 47 | + number_of_ways[0] = 1 # base case: 1 way to make 0 pence |
| 48 | + |
| 49 | + for coin in coins: |
| 50 | + for i in range(coin, pence + 1, 1): |
| 51 | + number_of_ways[i] += number_of_ways[i - coin] |
| 52 | + return number_of_ways[pence] |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + assert solution(200) == 73682 |
0 commit comments