Completed exercise 031

This commit is contained in:
Correl Roush 2011-04-06 23:29:56 -04:00
parent ceb8b9385a
commit aa3240015b

24
e031.py
View file

@ -10,11 +10,21 @@ It is possible to make £2 in the following way:
How many different ways can £2 be made using any number of coins? How many different ways can £2 be made using any number of coins?
""" """
if __name__ == '__main__': def combinations(amount, denominations):
coins = [1, 2, 5, 10, 20, 50, 100, 200]
coins = [200, 100, 50, 20, 10, 5, 2, 1]
combinations = 0
for start in range(len(coins)):
current = start
total = 0 total = 0
break if not denominations:
return total
denominations = sorted(denominations, reverse=True)
for i in range(len(denominations)):
d = denominations[i]
n = 1
while d * n <= amount:
if d * n == amount:
total += 1
total += combinations(amount - (d * n), denominations[i+1:])
n += 1
return total
if __name__ == '__main__':
coins = [200, 100, 50, 20, 10, 5, 2, 1]
print combinations(200, coins)