Skip to content

Instantly share code, notes, and snippets.

@hughdbrown
Created June 16, 2016 21:16
Show Gist options
  • Save hughdbrown/ad375f5f1bfe34673b05f4dbcb5b8b74 to your computer and use it in GitHub Desktop.
Save hughdbrown/ad375f5f1bfe34673b05f4dbcb5b8b74 to your computer and use it in GitHub Desktop.
Calculate number of unique ways to make change with a set of coins
def partial_soln(solns, coins, target):
solns[target] = []
for i in coins:
x = target - i
if x in solns:
for xx in solns[x]:
new_item = tuple(sorted(list(xx) + [i]))
solns[target].append(new_item)
def solution(coins, target):
solns = {0: [()]}
for i in range(1, target + 1):
partial_soln(solns, coins, i)
return solns[target]
def unique_solution(coins, target):
s = solution(coins, target_amt)
solutions = set(s)
return sorted(solutions)
if __name__ == '__main__':
# target_amt = 4
# coins = [1, 2, 3]
coins = [2, 5, 3, 6]
target_amt = 10
print(unique_solution(coins, target_amt))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment