Last active
August 29, 2015 14:01
-
-
Save debetimi/e05736a0dd5b3416151d to your computer and use it in GitHub Desktop.
This file contains 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
import copy | |
import functools | |
def memoize(obj): | |
cache = obj.cache = {} | |
@functools.wraps(obj) | |
def memoizer(*args, **kwargs): | |
key = str(args) + str(kwargs) | |
if key not in cache: | |
cache[key] = obj(*args, **kwargs) | |
return cache[key] | |
return memoizer | |
@memoize | |
def make_change(rem, unit): | |
if rem < 0: | |
return 0 | |
total = 0 | |
if unit == 1 or rem == 0 or rem == unit: | |
return 1 | |
if unit >= 5: | |
total += make_change(rem - unit, 1) | |
total += make_change(rem - unit, 5) | |
if unit >= 10: | |
total += make_change(rem - unit, 10) | |
if unit >= 25: | |
total += make_change(rem - unit, 25) | |
if unit == 50: | |
total += make_change(rem - unit, 50) | |
return total | |
def ways(number): | |
return make_change(number, 1) + make_change(number, 5) + make_change(number, 10) + make_change(number, 25) + make_change(number, 50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment