What is the difference between these 2 fragments of python codes?
I want to check whether an array contains the integers 1,2,3 in sequence as its elements?
def arrayCheck(nums):
for i in nums:
if(i <= (len(nums)-3)):
if (nums[i] == 1 and nums[i+1] == 2 and nums[i+2] == 3):
return(True)
return(False)
def arrayCheck(nums):
for i in range(0,len(nums)-2):
if (nums[i] == 1 and nums[i+1] == 2 and nums[i+2] == 3):
return(True)
return(False)
The first one gives wrong answer for the array:
arrayCheck([1,1,2,1,1,1,1,2,3])
but the second one is correct.