1

Question: Given a list of ints, return True if the array contains a 3 next to a 3 somewhere.

Exp: has_33([1, 3, 3]) → True

My Code:

def has_33(nums):
     for items in nums:
        if items == 3:
            if nums[nums.index(items)+1] == 3:
                return True
           
            else:
                continue
    
    return False

With this code I can't get get the answer I want. My answer I got now is: has_33([1, 3, 1, 3]) → False has_33([3, 1, 3]) → False has_33([3, 1, 3, 3]) → False #Even next to the "3" is "3" too.

OneCricketeer
  • 151,199
  • 17
  • 111
  • 216
Jack Wong
  • 23
  • 3
  • 3
    As to specifically why this doesn't work. Please read the [documentation](https://docs.python.org/3/library/array.html?highlight=array#array.array.index) of `index` and what it does. It should be very clear why this won't work – sinanspd Jan 03 '21 at 14:08

4 Answers4

2

.index() returns the first found index of the item. In the case of [3,1,3,3], 1 does not equal 3, so the loop continues.

You need different logic because if you had given input of [1,2,3], for example, where your first 3 is last in the list, you get an IndexError

One possible way: Iterating over every two elements in a list

OneCricketeer
  • 151,199
  • 17
  • 111
  • 216
1

index() method gives you the index that first occurrence of the item, using enumerate is more effective way to do this. check out here

def has_33(arr):
    for index, item in enumerate(arr[:-1]):
        if item == 3 and arr[index+1] == 3:
            return True

    return False
dolmushcu
  • 844
  • 4
  • 16
  • 1
    It would be more useful if you explained why the OP's code did not work, and how you have fixed it. – Rodney Jan 03 '21 at 14:09
0

.index gives you the index of the first occurrence of the value

>>> lst=[3,1,3,3]
>>> lst.index(3)
0
>>>

you can give it a second argument start so it gives you the position of the value counting from there

>>> lst.index(3,1)
2
>>> lst.index(3,0)
0
>>> lst.index(3,2)
2
>>> lst.index(3,3)
3
>>> 

but I would do it with zip to pair the elements with the next one and see if (3, 3) is there

>>> list(zip(lst, lst[1:]))
[(3, 1), (1, 3), (3, 3)]
>>> any((3, 3) == pair for pair in zip(lst, lst[1:]))
True
>>> 
Programmer
  • 5,276
  • 3
  • 9
  • 33
Copperfield
  • 7,242
  • 3
  • 17
  • 26
0

At the moment, you are searching for the first occurrence of 3 in the list of numbers, with nums.index. But in your examples, this will be the first 3 in the list, and not the third. This code will do what you want, and it works for any number, by giving it a second optional argument:

def has_xx(nums, x=3):
    for index, num in enumerate(nums):
        if num == x:
            try:
                if nums[index + 1] == x:
                    return True
               
                else:
                    continue
            except IndexError:
                continue
    return False

Works as follows:

>>> has_xx([1, 2, 3, 3, 5])
True
>>> has_xx([1, 2, 3, 4, 4, 3])
False
>>> has_xx([1, 2, 3, 4, 4, 3], x=4)
True
>>> has_xx([1, 2, 3, 4, 4, 3], x=3)
False
Programmer
  • 5,276
  • 3
  • 9
  • 33