I am trying to implement bestSum algorithm in python and I have the following script with the issue at line 13,14.
memo = {}
def bestSum(s, l):
if s == 0:
return []
elif s < 0:
return None
elif s in memo:
return memo[s]
minList = None
for i in l:
temp = bestSum(s-i, l)
if temp != None:
temp = temp + [i] #This works LINE13
# temp.append(i) This does not work LINE14
if minList == None or len(temp) < len(minList):
minList = temp
memo[s] = minList
return minList
I know that "+" operator return a new list while the append function mutates the original list.
My question is why does it matter in my code as the temp list is only being used in the next few lines. Why are they giving different outputs.