the memoization can't be initiated. if I make it memo
global, then the subsequent print function will take stored memo during the first print. Please advise,
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