Created
November 17, 2016 16:24
-
-
Save spedy/6114f42998379009ed861e50ce10d8c7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def contains_sequence(array, sequence): | |
for idx in range(len(array)): | |
if idx + len(sequence) > len(array): | |
return False | |
if array[idx:idx+len(sequence)] == sequence: | |
return True | |
# Tests | |
tests = { | |
"contains_sequence": { | |
"sequence": [1, 3, 4], | |
"array": [1, 3, 4], | |
"result": True | |
}, | |
"not_contains_sequence": { | |
"sequence": [1, 3, 4], | |
"array": [1, 2, 4], | |
"result": False | |
}, | |
"not_contains_shorter_array": { | |
"sequence": [1, 3, 4], | |
"array": [1, 2], | |
"result": False | |
}, | |
"not_contains_longer_array": { | |
"sequence": [1, 3, 4], | |
"array": [1, 2, 3, 4, 5], | |
"result": False | |
}, | |
"contains_longer_array": { | |
"sequence": [1, 3, 4], | |
"array": [8, 1, 3, 4, 5], | |
"result": True | |
} | |
} | |
for t in tests: | |
assert tests[t]["result"] == contains_sequence(tests[t]["array"], tests[t]["sequence"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment