Python: 无法在函数中调用 Memoization
Python: Memoization can't be called in Function
无法启动记忆。如果我将其设置为 memo
global,那么随后的打印功能将在第一次打印时获取存储的备忘录。请指教,
def howsumhelper(targetsum,numbers):
memo = dict() #this memoization will not initiate. Why?
return howsum(targetsum,numbers,[])
def howsum(targetsum,numbers,combo):
print("Debug==",memo)
if targetsum in memo: return memo[targetsum]
if targetsum == 0:return combo
if targetsum < 0: return None
for number in numbers:
remainder = targetsum - number
if howsum(remainder,numbers,combo) != None:
combo.append(number)
memo[targetsum] = combo
return combo
memo[targetsum] = None
return None
print(howsumhelper(7,[3,4])) #output should be [3,4]
print(howsumhelper(8,[2,3])) #output should be [2,2,2,2]
print(howsumhelper(7,[2,4])) #output should be None
建议进行以下更改
- 使 memo 成为 howsum 的可变默认参数(使其在函数中可调用)
- 你不需要 howsumhelper
Reference--Efficient memorization in Python--使用发布的解决方案 #2
修改后的代码
def howsum(targetsum,numbers,combo=None,memo=None):
# Initiaze default arguments
if combo is None:
combo = []
if memo is None:
memo = {}
#print("Debug==",memo)
if targetsum in memo: return memo[targetsum]
if targetsum == 0:return combo
if targetsum < 0: return None
for number in numbers:
remainder = targetsum - number
if howsum(remainder,numbers,combo,memo) != None:
combo.append(number)
memo[targetsum] = combo
return combo
memo[targetsum] = None
return None
print(howsum(7,[3,4])) #output should be [3,4] #actually or [4, 3]
print(howsum(8,[2,3])) #output should be [2,2,2,2]
print(howsum(7,[2,4])) #output should be None
输出
[4, 3]
[2, 2, 2, 2]
None
说明
函数签名是:howsum(targetsum,numbers,combo=None,memo=None):
函数定义中的参数值称为默认参数。
所以 combo 和 memo 都有默认参数。
Python 的默认参数在定义函数时计算一次,而不是每次调用函数时计算一次。
只有在传递实际值时才使用默认值。
因此:
howsum(7,[3,4]) # uses default values, so called with combo = None and memo = None
howsum(8,[2,3]) # uses default values, so called with combo = None and memo = None
howsum(7,[2,4]) # uses default values, so called with combo = None and memo = None
但是:
howsum(remainder,numbers,combo,memo) # does not use defaults for combo and memo
# since we are passing values for these arguments
无法启动记忆。如果我将其设置为 memo
global,那么随后的打印功能将在第一次打印时获取存储的备忘录。请指教,
def howsumhelper(targetsum,numbers):
memo = dict() #this memoization will not initiate. Why?
return howsum(targetsum,numbers,[])
def howsum(targetsum,numbers,combo):
print("Debug==",memo)
if targetsum in memo: return memo[targetsum]
if targetsum == 0:return combo
if targetsum < 0: return None
for number in numbers:
remainder = targetsum - number
if howsum(remainder,numbers,combo) != None:
combo.append(number)
memo[targetsum] = combo
return combo
memo[targetsum] = None
return None
print(howsumhelper(7,[3,4])) #output should be [3,4]
print(howsumhelper(8,[2,3])) #output should be [2,2,2,2]
print(howsumhelper(7,[2,4])) #output should be None
建议进行以下更改
- 使 memo 成为 howsum 的可变默认参数(使其在函数中可调用)
- 你不需要 howsumhelper
Reference--Efficient memorization in Python--使用发布的解决方案 #2
修改后的代码
def howsum(targetsum,numbers,combo=None,memo=None):
# Initiaze default arguments
if combo is None:
combo = []
if memo is None:
memo = {}
#print("Debug==",memo)
if targetsum in memo: return memo[targetsum]
if targetsum == 0:return combo
if targetsum < 0: return None
for number in numbers:
remainder = targetsum - number
if howsum(remainder,numbers,combo,memo) != None:
combo.append(number)
memo[targetsum] = combo
return combo
memo[targetsum] = None
return None
print(howsum(7,[3,4])) #output should be [3,4] #actually or [4, 3]
print(howsum(8,[2,3])) #output should be [2,2,2,2]
print(howsum(7,[2,4])) #output should be None
输出
[4, 3]
[2, 2, 2, 2]
None
说明
函数签名是:howsum(targetsum,numbers,combo=None,memo=None):
函数定义中的参数值称为默认参数。 所以 combo 和 memo 都有默认参数。
Python 的默认参数在定义函数时计算一次,而不是每次调用函数时计算一次。
只有在传递实际值时才使用默认值。
因此:
howsum(7,[3,4]) # uses default values, so called with combo = None and memo = None
howsum(8,[2,3]) # uses default values, so called with combo = None and memo = None
howsum(7,[2,4]) # uses default values, so called with combo = None and memo = None
但是:
howsum(remainder,numbers,combo,memo) # does not use defaults for combo and memo
# since we are passing values for these arguments