Created
September 9, 2024 19:08
-
-
Save ariel-co/55c03274e55c6083b2c2987c2178497e to your computer and use it in GitHub Desktop.
test subarray in Python
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 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