简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM