From aa3240015b7a973c7dd0704676322f95ecd009e4 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Wed, 6 Apr 2011 23:29:56 -0400 Subject: [PATCH] Completed exercise 031 --- e031.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/e031.py b/e031.py index 6b1f162..cf58638 100644 --- a/e031.py +++ b/e031.py @@ -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? """ +def combinations(amount, denominations): + total = 0 + 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 = [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 - break + print combinations(200, coins)