Code:-
def canSum(targetSum, numbers, d={}):
if targetSum in d.keys():
return d[targetSum]
if targetSum == 0:
return True
if targetSum < 0:
return False
for each in numbers:
rem = targetSum - each
if canSum(rem, numbers, d):
d[targetSum] = True
return True
d[targetSum] = False
return False
MY ISSUE-
When I run the above code for the testcase - print(canSum(7, [2, 4])), it return false(which is correct). But when I run the same code for the two testcases that are - print(canSum(7, [3, 5, 4])) and print(canSum(7, [2, 4])), it returns true for both!(which is wrong).
I don't know what is happening. Is there something wrong with the code? Help me out.