可以遍历整个列表,如果遇到0,那么继续遍历直到找到第二个0或1,再继续遍历直到找到7,如果按照顺序找到了这三个数字,则返回True,否则返回False。具体实现如下:
def contains_007(numbers):
index = 0
for number in numbers:
if number == 0 and index == 0:
index += 1
elif number == 0 and index == 1:
index += 1
elif number == 7 and index == 2:
return True
return False
# 测试
assert contains_007([0, 0, 7, 1, 2, 4, 0, 0, 7]) == True
assert contains_007([1, 2, 4, 0, 0, 7]) == False