Skip to content

Instantly share code, notes, and snippets.

@ariel-co
Created September 9, 2024 19:08
Show Gist options
  • Select an option

  • Save ariel-co/55c03274e55c6083b2c2987c2178497e to your computer and use it in GitHub Desktop.

Select an option

Save ariel-co/55c03274e55c6083b2c2987c2178497e to your computer and use it in GitHub Desktop.
test subarray in Python
def is_subarray(a, b):
return any(
all( e_a==e_b for e_a, e_b in zip(a,b[i:]) )
for i in range(len(b)-len(a)+1) )
assert is_subarray('ab', 'abcd')
assert is_subarray('bc', 'abcd')
assert is_subarray('cd', 'abcd')
assert not is_subarray('de', 'abcd')
assert not is_subarray('abcd', 'ab')
assert is_subarray('', 'abcd')
assert not is_subarray('abcd', '')
assert is_subarray('', '')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment