'Write a function that takes in a list of integers and returns True if it contains 007 in order but they don't have to be consecutive

examples :

1.spy_game([1,2,4,0,0,7,5]) --> True

2.spy_game([1,0,2,4,0,5,7]) --> True

3.spy_game([1,7,2,0,4,5,0]) --> False



Solution 1:[1]

By converting the list to string you can make a pattern-matching with a regular expression:

import re


def spy_game(lst: list) -> bool:
    lst_as_str = ''.join(map(str, lst))
    if re.search(r'(0\d*0\d*7)', lst_as_str):
        return True
    else:
        return False

l1 = [1,2,4,0,0,7,5]
l2 = [1,0,2,4,0,5,7]
l3 = [1,7,2,0,4,5,0]
print(spy_game(l1))
print(spy_game(l2))
print(spy_game(l3))

Solution 2:[2]

def spy_game(mylist):
my007list = []
for item in mylist:
    if item == 0 or item == 7:
        my007list.append(item)
print(my007list)

for item in range(len(my007list)):
    if my007list[item] == 0 and my007list[item+1] == 0 and my007list[item+2] == 7:
        return True
    return False

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cards
Solution 2