Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Given an array of ints, return True if the sequence of numbers 1, 2, 3 appears in the array somewhere. array123([1, 1, 2, 3, 1]) → True array123([1, 1, 2, 4, 1]) → False array123([1, 1, 2, 1, 2, 3]) → True
tests = {"1":[1,1,2,3,1],"2":[1,1,2,4,1],"3":[1,1,2,1,2,3]}
def array123(array):
if len(array) < 3:
return False
for (index, item) in enumerate(array):
if item == 1:
state1 = True
elif (item == 2) and state1:
state2 = True
elif (item == 3) and state2:
return True
else:
state1 = False
state2 = False
if index == len(array) - 1:
return False
for test in tests:
print "starting test %s" % test
print array123(tests[test])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment