Skip to content

Instantly share code, notes, and snippets.

@talhaahussain
Last active June 12, 2024 02:49
Show Gist options
  • Save talhaahussain/bad86fce26afdc239af4af9eab23167b to your computer and use it in GitHub Desktop.
Save talhaahussain/bad86fce26afdc239af4af9eab23167b to your computer and use it in GitHub Desktop.
contiguous_subarrays.py -- A simple, type-hinted solution using list comprehension for generating all contiguous subarrays of any homogeneous or non-homogeneous list across one dimension. Does not return the original list.
def generate_subarrays(a: list[any]) -> list[any]:
return [a[i:j] for i in range(len(a)) for j in range(i + 1, len(a) + 1)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment