Created
June 16, 2016 21:16
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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