Memoizing Coin Change

岁酱吖の 提交于 2019-11-27 16:23:05

It's not necessary to write a specialized memorizing decorator when you could just use a generic pre-written one...such as the following straight from the PythonDecoratorLibrary:

import collections
import functools

class memoized(object):
   '''Decorator. Caches a function's return value each time it is called.
   If called later with the same arguments, the cached value is returned
   (not reevaluated).
   '''
   def __init__(self, func):
      self.func = func
      self.cache = {}
   def __call__(self, *args):
      if not isinstance(args, collections.Hashable):
         # uncacheable. a list, for instance.
         # better to not cache than blow up.
         return self.func(*args)
      if args in self.cache:
         return self.cache[args]
      else:
         value = self.func(*args)
         self.cache[args] = value
         return value
   def __repr__(self):
      '''Return the function's docstring.'''
      return self.func.__doc__
   def __get__(self, obj, objtype):
      '''Support instance methods.'''
      return functools.partial(self.__call__, obj)

You could then apply it to yourchange()function (or any other, since it's generic) like this:

@memoized
def change(a, kinds=(50, 20, 10, 5, 1)):
    if a == 0:
        return 1
    if a < 0 or len(kinds) == 0:
        return 0

    return change(a - kinds[0], kinds) + change(a, kinds[1:])

print(change(10))  # 4

It doesn't answer your question as asked, but if r[0] to r[i] are the number of ways of making change with the first k of your denominations, then r[i+1] is the number of ways of making change with the first k-1 denominations plus r[i-k]. This leads to an elegant solution to the problem you're solving:

def change(total, denominations):
    r = [1] + [0] * total
    for k in denominations:
        for i in xrange(k, len(r)):
            r[i] += r[i - k]
    return r[total]

print change(100, (50, 20, 10, 5, 1))

This approach is discussed in Polya's book "How to solve it". In general, using memoisation to ameliorate recursive solutions is a simple way to code dynamic programming solutions in Python, but my personal opinion is that it's an important skill to be able to drop down a level and figure out exactly how to build the intermediate results in a table in a dynamic programming solution. Often (and exemplified here), the result is faster and way simpler to read (although harder to code in the first place).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!