Advertisement
benlloydjones

Project Euler 31

Jun 17th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. '''In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:
  2.  
  3. 1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
  4. It is possible to make £2 in the following way:
  5.  
  6. 1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
  7. How many different ways can £2 be made using any number of coins?'''
  8.  
  9. def coinCombi(n):
  10.     import copy
  11.     coins = {'1': 0, '2': 0, '5': 0, '10': 0, '20': 0, '50': 0, '100': 0, '200': 0}
  12.     solution = []
  13.     for value in range(1, n + 1, 1):
  14.         valueSolve = []
  15.         for coin in coins:
  16.             if int(coin) < value:
  17.                 diff = value - int(coin)
  18.                 newSolve = copy.deepcopy(solution[diff - 1])
  19.                 for solve in newSolve:
  20.                     solve[coin] = solve[coin] + 1
  21.                 for x in newSolve:
  22.                     if x not in valueSolve:
  23.                         valueSolve.append(x)
  24.             elif value - int(coin) == 0:
  25.                 currentSolve = copy.deepcopy(coins)
  26.                 currentSolve[coin] = currentSolve[coin] + 1
  27.                 valueSolve.append(currentSolve)
  28.         solution.append(valueSolve)
  29.     return len(solution[-1])
  30.  
  31. coinCombi(200)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement