【问题标题】:Memoizing Coin Change记忆硬币变化
【发布时间】:2014-01-11 14:57:29
【问题描述】:

我想将我的硬币找零功能转换为记忆功能
为此,我决定使用字典,这样我的字典中的键将是硬币,值将是一个列表,其中包含所有可以更改“键”硬币的硬币。
我所做的是:

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:])


def memoizeChange(f):
    cache={}
    def memo(a,kinds=(50,20,10,5,1)):

        if len(cache)>0 and kinds in cache[a]:
            return 1
        else:
            if(f(a,kinds)==1):
                cache[a]=kinds // or maybe cache[a].append(..)
                return cache[a]+memo(a-kinds[0],kinds)+memo(a,kinds[1:])
    return memo

memC=memoizeChange(change)
kinds=(50,20,10,5,1)
print(memC(10,kinds))

我想得到一些建议,或者也许有其他方法可以做到这一点。
谢谢。


编辑
备忘版:

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:])


def memoizeChange(f):
    cache={}
    def memo(a,kinds=(50,20,10,5,1)):
        if not (a,kinds) in cache:
                cache[(a,kinds)]=f(a,kinds)
        return cache[(a,kinds)]
    return memo

change=memoizeChange(change)
print(change(10))

【问题讨论】:

  • 由于您是递归调用函数,请考虑将 memoization 注入函数本身,并递归调用 memoized 函数,这样您在请求新值时将重用现有的 memoized 函数结果。
  • @perreal “重复”很好理解它的想法,我面临的是如何保存重复值并避免它的方法。不过还是谢谢。

标签: python memoization


【解决方案1】:

它没有按要求回答您的问题,但如果 r[0] 到 r[i] 是用您的前 k 个面额进行更改的方式的数量,那么 r[i+1] 是数字使用前 k-1 个面额加上 r[ik] 进行更改的方法。这会为您正在解决的问题提供一个优雅的解决方案:

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))

Polya 的“如何解决它”一书中讨论了这种方法。一般来说,使用记忆来改善递归解决方案是在 Python 中编写动态编程解决方案的一种简单方法,但我个人的看法是,能够降低一个级别并弄清楚如何构建中间结果是一项重要的技能动态规划解决方案中的表。通常(并在此处举例说明),结果更快且更易于阅读(尽管首先更难编码)。

【讨论】:

    【解决方案2】:

    当您可以使用通用的预写装饰器时,没有必要编写专门的记忆装饰器...例如直接来自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)
    

    然后您可以将它应用到您的change()function(或任何其他,因为它是通用的),如下所示:

    @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
    

    【讨论】:

    • 好的,谢谢,顺便说一句,我不知道我可以使用它,但在你回答我成功之前,请查看编辑
    • 是的,将结果分配给原始函数的名称是 @decorator 语法为您所做的——这样做通常意味着您不必更改所有现有调用来使用修改后的版本具有不同的名称。
    猜你喜欢
    • 1970-01-01
    • 2015-07-03
    • 2018-10-07
    • 2019-07-26
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 2016-01-24
    • 2017-10-28
    相关资源
    最近更新 更多