Skip to content

Instantly share code, notes, and snippets.

@ylegall
Created May 22, 2016 22:27
Show Gist options
  • Save ylegall/66751210c1e9e23ae8d0f6abc1dd64b0 to your computer and use it in GitHub Desktop.
Save ylegall/66751210c1e9e23ae8d0f6abc1dd64b0 to your computer and use it in GitHub Desktop.
number of ways to make change from coins without repetition
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