I was working in a small problem in python. This is the problem -
Given an array of ints, compute recursively if the array contains a 6. We'll use the convention of considering only the part of the array that begins at the given index. In this way, a recursive call can pass index+1 to move down the array. The initial call will pass in index as 0.
array6([1, 6, 4], 0) → true
array6([1, 4], 0) → false
array6([6], 0) → true
This is the solution which i came up with.
def array6(intList, index):
if len(intList) < index + 1:
print(False)
else:
if intList[index] == 6:
print(True)
else:
array6(intList, index + 1)
The script seems to work correctly, except when i change print to return statements and call it as : print(array6([1, 6, 4], 0)) it displays "None" in the console. Why is this? It should ideally display True or False - Right? Or am i missing something in the script?