Skip to content

Instantly share code, notes, and snippets.

@walkingpendulum
Created June 29, 2021 18:28
Show Gist options
  • Save walkingpendulum/383bd4f5b37d7ccd8f3b52ff3e704bc9 to your computer and use it in GitHub Desktop.
Save walkingpendulum/383bd4f5b37d7ccd8f3b52ff3e704bc9 to your computer and use it in GitHub Desktop.
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