Skip to content

Commit 3ac45e4

Browse files
committed
Create make_change.py
1 parent cbe8b58 commit 3ac45e4

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

make_change.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
def make_change(amount, denominations, index=0):
2+
""" Write a function that, given:
3+
1. an amount of money
4+
2. a list of coin denominations
5+
computes the number of ways to make the amount of money with coins of the available denominations.
6+
7+
>>> make_change(amount=4, denominations=[1,2,3])
8+
4
9+
[1,1,1,1]
10+
[1,1,2]
11+
[1,3]
12+
[2,2]
13+
14+
>>> make_change(amount=20, denominations=[5, 10])
15+
3
16+
[5,5,5,5]
17+
[5,5,10]
18+
[10,10]
19+
20+
"""
21+
22+
23+
# time:
24+
# space:
25+
26+
27+
if amount == 0:
28+
return 1
29+
30+
if amount < 0:
31+
return 0
32+
33+
if index == len(denominations):
34+
return 0
35+
36+
current_coin = denominations[index]
37+
38+
combos = 0
39+
40+
while amount >=0:
41+
combos += make_change(amount, denominations, index+1)
42+
amount -=current_coin
43+
44+
return combos
45+
46+
47+
48+
if __name__ == '__main__':
49+
import doctest
50+
results = doctest.testmod()
51+
if not results.failed:
52+
print 'All tests passed!'

0 commit comments

Comments
 (0)