Skip to content

Instantly share code, notes, and snippets.

@goldsborough
Created October 7, 2015 15:33
Show Gist options
  • Save goldsborough/e27f38ff89ac413a07fb to your computer and use it in GitHub Desktop.
Save goldsborough/e27f38ff89ac413a07fb to your computer and use it in GitHub Desktop.
A python function to memoize DP problems
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