I'm trying to figure out why the code I wrote to find a sequence of integers in any given array is not catching all the instances. Could you help, please?
Here's the code:
def array123(nums):
if len(nums) < 3:
return False
else:
for i in range(len(nums) - 1):
if nums[i:i 3] == [1, 2, 3]:
return True
return False
CodePudding user response:
The return False
part should be indented as follows.
def array123(nums):
if len(nums) < 3:
return False
else:
for i in range(len(nums) - 1):
if nums[i:i 3] == [1, 2, 3]:
return True
return False # This should be indented this way.
CodePudding user response:
def array123(nums):
nums_to_string = str(nums)
if len(nums_to_string) < 3:
return False
else:
for i in range(len(nums_to_string) - 1):
if nums_to_string[i:i 3] == "123":
return True
return False