I wrote this code and it was working well before I memoized it? Its supposed to return true if its possible to sum up to the targetSum using the numbers in the array, and false when its not possible
def can_sum(targetSum,numbers,memo={}):
if targetSum<0:
return False
if targetSum in memo:
return memo[targetSum]
if targetSum==0:
return True
if targetSum<0:
return False
for n in numbers:
remainder = targetSum-n
if can_sum(remainder,numbers,memo)==True:
memo[targetSum]=True
return True
memo[targetSum]=False
return False
#all of the values below are coming out to be true
print(can_sum(7,[2,3])) #true ------
print(can_sum(7,[7,5,3,4]))#true |
print(can_sum(7,[2,4])) #false |----[Actual Values]
print(can_sum(8,[2,3,5])) #true |
print(can_sum(300,[7,14])) #false ------
Please provide the solution