Last active
July 21, 2019 22:12
-
-
Save tlang0/dda6ec2eb4200666f8a7 to your computer and use it in GitHub Desktop.
Write a function that counts how many different ways you can make change for an amount of money, given an array of coin denominations. For example, there are 3 ways to give change for 4 if you have coins with denomination 1 and 2: 1+1+1+1, 1+1+2, 2+2.
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
combinations = [] | |
amount = 12 | |
def count_denoms(denoms, comb): | |
global combinations | |
for i in range(len(denoms)): | |
comb_new = comb + [denoms[i]] | |
if sum(comb_new) < amount: | |
count_denoms(denoms[i:], comb_new) | |
elif sum(comb_new) == amount: | |
print(comb_new) | |
combinations.append(comb_new) | |
if __name__ == "__main__": | |
count_denoms([2, 5, 1], []) | |
print("Number of combinations: " + str(len(combinations))) |
uwevanopfern
commented
Jan 9, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment