Skip to content

Commit cf76dac

Browse files
SandersLingithub-actionsitsvinayakpoyea
authored andcommitted
Create sol2.py (TheAlgorithms#1876)
* Create sol2.py * updating DIRECTORY.md * Update DIRECTORY.md * updating DIRECTORY.md * Update sol2.py * Update DIRECTORY.md * updating DIRECTORY.md * Improve docstrings Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: vinayak <[email protected]> Co-authored-by: John Law <[email protected]>
1 parent e4d23c0 commit cf76dac

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@
509509
* [Soln](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_30/soln.py)
510510
* Problem 31
511511
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_31/sol1.py)
512+
* [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_31/sol2.py)
512513
* Problem 32
513514
* [Sol32](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_32/sol32.py)
514515
* Problem 33

project_euler/problem_31/sol2.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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

Comments
 (0)