From ceb8b9385ab8f52db03ef184c0dfb76bdff4522e Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Sat, 2 Apr 2011 00:27:16 -0400 Subject: [PATCH 1/2] Problem 031 - WIP --- e031.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 e031.py diff --git a/e031.py b/e031.py new file mode 100644 index 0000000..6b1f162 --- /dev/null +++ b/e031.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +""" +In England the currency is made up of pound, £, and pence, p, and there are +eight coins in general circulation: + 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p). + +It is possible to make £2 in the following way: + 1£1 + 150p + 220p + 15p + 12p + 31p + +How many different ways can £2 be made using any number of coins? +""" + +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 From aa3240015b7a973c7dd0704676322f95ecd009e4 Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Wed, 6 Apr 2011 23:29:56 -0400 Subject: [PATCH 2/2] 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)