Right code but still test case is not running in python
<p>Given an array of ints, return True if .. 1, 2, 3, .. appears in the array somewhere.</p>

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

<p><a href="http://codingbat.com/prob/p193604" rel="nofollow">Coding bat problem</a></p>

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

<p>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 <em>slicing</em> does not raise errors, when done correclty, irrespective of start and/or stop indices.</p>

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

<pre><code>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])
</code></pre>

<hr>

<p>Useful reference:</p>

<p><a href="http://stackoverflow.com/questions/509211/explain-pythons-slice-notation">Explain Python&#39;s slice notation</a></p>

python python-3.x