Home > Software design >  Write a function that takes in a list of integers and returns True if it contains 007 in order but t
Write a function that takes in a list of integers and returns True if it contains 007 in order but t

Time:04-19

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

CodePudding user response:

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))

CodePudding user response:

def spy_game(nums):
x = []
for i , l in enumerate(nums):
    if l == 0:
        x.append(nums[i])
    if l == 7:
        x.append(nums[i])
print(x)
if x[0] == 0 and x[1] == 0 and x[2] == 7:
    return True
return False
  • Related