-2

Given an array of ints, return True if .. 1, 2, 3, .. appears in the array somewhere.

def array123(nums):
    for i in  nums:
       if nums[i:i+3] == [1,2,3]:
          return True
    return False

Coding bat problem

my code is satisfying all the test cases except for nums=[1,2,3] can someone tell me whats wrong with my code

3 Answers3

1

Your code is not completely right. You're slicing the list with the items in the list, not with indices. Luckily, this did not throw any error because the items in the list are within the bounds of the list's indices or rather slicing does not raise errors, when done correclty, irrespective of start and/or stop indices.

You can use range(len(...)) to generate the indices, and you may stop the search at len(nums) - len(sublist), so you don't check for slices less than the length of the sublist. This comes more handy as the length of the sublist gets larger.

def array123(nums, sublist):
    j = len(sublist)
    for i in range(len(nums)-j):
       if nums[i:i+j] == sublist:
          return True
    return False

# Call function
array123(nums, [1,2,3])

Useful reference:

Explain Python's slice notation

Community
  • 1
  • 1
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
  • I don't understand the part "slicing the list with the items in the list, not with indices" . Can you explain –  Oct 08 '16 at 20:42
  • @Duck_dragon `for i in nums` iterates on elements in the list, and doing `nums[i:i+3]` uses those elements as *slice indices* – Moses Koledoye Oct 09 '16 at 04:42
  • But then if this is happening behind the scenes then why i am getting correct answers for all the previous problems. And in this one also. Only the thing is that one test case is not working and rest all are working. –  Oct 09 '16 at 06:40
  • can you provide me with a link where i can read about slicing indices and elements in list –  Oct 09 '16 at 06:41
  • also len(nums) is giving me error:list index out of range while len(nums)-2 is not can you explain why –  Oct 09 '16 at 07:35
  • This would help with understanding slicing: [Explain Python's slice notation](http://stackoverflow.com/questions/509211/explain-pythons-slice-notation) – Moses Koledoye Oct 09 '16 at 13:37
1

It should be like this.

def array123(nums):
    for i in range(len(nums)):
        if nums[i:i+3] == [1,2,3]:
            return True
    return False

Try this. Hope this helps. :)

Harv
  • 446
  • 5
  • 12
0

You're getting wrong result because you're iterating on the value of elements not on the index of elements of elements. Try following code

def array123(nums):
    for i in range(len(nums)-2):
       if nums[i:i+3] == [1,2,3]:
          return True
    return False

And remember to give the end index of list (range(len(nums)-2)) because suppose if length of your array is 4 then (range(len(nums)-2)) will be

(range(2)) = [0,1]

So the loop will iterate for 0,1 as starting index

  • Why you are using len(nums)-2 or num[:-2] what is this concept .why i can't just iterate over the list as whole –  Oct 09 '16 at 07:28
  • also len(nums) is giving me error:list index out of range while len(nums)-2 is not can you explain why –  Oct 09 '16 at 07:35
  • because let's suppose len(nums)=5 then if you have len(nums) in place of (len(nums)-2) then range(len(nums))=[0,1,2,3,4] and next line in loop nums[i:i+3] = nums[4:4+3] for last index 4 and thus nums[4:7] which is not possible because nums contain elements only till 4th index. – Sunny Aggarwal Oct 10 '16 at 06:48