Without using a For Loop, I need to recursively check if any two numbers in a list add to 0. If it does, it will return True, else, it will return False. For example, [12, 5, 10, -5, -9] returns true as 5 + -5 = 0. However, [12, 8, 10, -5] would return false, because no two numbers add to 0.
def testForZero(L)
if len(L) <= 2:
return False
else:
if L[0] + testForZero(L[1]) == 0:
return True
else:
return testForZero(L[1:])
I'm sorry, I'm pretty new to coding but my thought process was this: I would add the first number in the list (L) to the next number. If the sum wasn't zero, I would move on to the next number. May I ask how I would get it so that I can add every single number in a list to each other recursively, and without a for loop...?