Created
March 2, 2014 07:04
-
-
Save GoWind/9303008 to your computer and use it in GitHub Desktop.
Coin change problem - change a given amount into coins of given denominations
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
| 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