Created
May 22, 2016 22:27
-
-
Save ylegall/66751210c1e9e23ae8d0f6abc1dd64b0 to your computer and use it in GitHub Desktop.
number of ways to make change from coins without repetition
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 make_change(amount, coins): | |
coins.sort() | |
def _make_change(amount, idx, result): | |
if amount == 0: | |
print result | |
return | |
for i in range(idx, len(coins)): | |
coin = coins[i] | |
if coin <= amount: | |
#print "CALL: (%s,%s,%s)" % (amount - coin, i, result + [coin]) | |
_make_change(amount - coin, i, result + [coin]) | |
_make_change(amount, 0, []) | |
# example: | |
make_change(4, [1,2,3]) | |
# [1, 1, 1, 1] | |
# [1, 1, 2] | |
# [1, 3] | |
# [2, 2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment