Skip to content

Instantly share code, notes, and snippets.

@GoWind
Created March 2, 2014 07:04
Show Gist options
  • Save GoWind/9303008 to your computer and use it in GitHub Desktop.
Save GoWind/9303008 to your computer and use it in GitHub Desktop.
Coin change problem - change a given amount into coins of given denominations
d = {}
def coinchange(change , Coinlist):
""" input
change - the amount that is to be changed into coins
Coinlist - list of coins sorted in descending order
output
list of possible combinations for given change
"""
if change == 0 :
return [[]]
if change in d:
return d[change]
sequences = []
for x in Coinlist:
if x <= change :
for seq in coinchange(change-x,Coinlist):
sequences.append([x]+seq)
d[change] = sequences
return sequences
def test_coinchange(change , Coinlist):
return coinchange(change,sorted(Coinlist,reverse=True))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment