Created
October 7, 2015 15:33
-
-
Save goldsborough/e27f38ff89ac413a07fb to your computer and use it in GitHub Desktop.
A python function to memoize DP problems
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
def memoize(cache_index=0): | |
def decorator(function): | |
cache = {} | |
def proxy(*args, **kwargs): | |
key = args[cache_index] | |
if key not in cache: | |
cache[key] = function(*args, **kwargs) | |
return cache[key] | |
return proxy | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment