MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_031/sol2.py:35–55  ·  view source on GitHub ↗

Returns the number of different ways to make X pence using any number of coins. The solution is based on dynamic programming paradigm in a bottom-up fashion. >>> solution(500) 6295434 >>> solution(200) 73682 >>> solution(50) 451 >>> solution(10) 11

(pence: int = 200)

Source from the content-addressed store, hash-verified

33
34
35def solution(pence: int = 200) -> int:
36 """Returns the number of different ways to make X pence using any number of coins.
37 The solution is based on dynamic programming paradigm in a bottom-up fashion.
38
39 >>> solution(500)
40 6295434
41 >>> solution(200)
42 73682
43 >>> solution(50)
44 451
45 >>> solution(10)
46 11
47 """
48 coins = [1, 2, 5, 10, 20, 50, 100, 200]
49 number_of_ways = [0] * (pence + 1)
50 number_of_ways[0] = 1 # base case: 1 way to make 0 pence
51
52 for coin in coins:
53 for i in range(coin, pence + 1, 1):
54 number_of_ways[i] += number_of_ways[i - coin]
55 return number_of_ways[pence]
56
57
58if __name__ == "__main__":

Callers 1

sol2.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected