Skip to content

Instantly share code, notes, and snippets.

@pixyj
Last active December 11, 2015 04:58
Show Gist options
  • Save pixyj/4548988 to your computer and use it in GitHub Desktop.
Save pixyj/4548988 to your computer and use it in GitHub Desktop.
def ways(amount, coin_types):
"""
Python implementation for the counting_change problem
http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-11.html#%_sec_1.2
Doesn't work for large numbers because we don't have tail call optimization in python
Time complexity analysis: Found an explanation at
http://wqzhang.wordpress.com/2009/06/09/sicp-exercise-1-14/
"""
if amount == 0:
return 1
if amount < 0 or len(coin_types) == 0:
return 0
coin_copy = list(coin_types)
coin_copy.remove(coin_types[0])
without_first = ways(amount, coin_copy)
with_first = ways(amount - coin_types[0], coin_types)
return without_first + with_first
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment