Created
June 29, 2021 18:28
-
-
Save walkingpendulum/383bd4f5b37d7ccd8f3b52ff3e704bc9 to your computer and use it in GitHub Desktop.
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
haystack = [1, 2, 3, 2, 1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 6] | |
needle = [2, 1, 2] | |
def cut_substr_once(substr, string): | |
substr_size = len(substr) | |
for ind, _ in enumerate(string): | |
if substr == string[ind: ind + substr_size]: | |
return string[:ind], string[ind + substr_size:] | |
return string, None | |
def cut_substr(substr, string): | |
tail, result = string, [] | |
while tail: | |
head, tail = cut_substr_once(substr, tail) | |
result.extend(head) | |
return result | |
print(cut_substr(needle, haystack)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment