Created
August 1, 2017 18:52
-
-
Save managedkaos/cf76cd5d4c33689f47779adaee2cb42f to your computer and use it in GitHub Desktop.
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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