Skip to content

Instantly share code, notes, and snippets.

@EkremDincel
Created June 13, 2021 19:07
Show Gist options
  • Save EkremDincel/4bdd2a144341b1919dc301b9a0d57037 to your computer and use it in GitHub Desktop.
Save EkremDincel/4bdd2a144341b1919dc301b9a0d57037 to your computer and use it in GitHub Desktop.
def windows(sequence, step):
i = 0
while i < len(sequence):
yield sequence[i: i + step]
i += step
def filled_windows(sequence, step):
i = 0
while i + step <= len(sequence):
yield sequence[i: i + step]
i += step
assert list(windows([1,2,3,4,5], 1)) == [[1], [2], [3], [4], [5]]
assert list(windows([1,2,3,4,5], 2)) == [[1, 2], [3, 4], [5]]
assert list(windows([1,2,3,4,5], 3)) == [[1, 2, 3], [4, 5]]
assert list(windows([1,2,3,4,5], 4)) == [[1, 2, 3, 4], [5]]
assert list(windows([1,2,3,4,5], 5)) == [[1, 2, 3, 4, 5]]
assert list(windows([1,2,3,4,5], 6)) == [[1, 2, 3, 4, 5]]
assert list(filled_windows([1,2,3,4,5], 1)) == [[1], [2], [3], [4], [5]]
assert list(filled_windows([1,2,3,4,5], 2)) == [[1, 2], [3, 4]]
assert list(filled_windows([1,2,3,4,5], 3)) == [[1, 2, 3]]
assert list(filled_windows([1,2,3,4,5], 4)) == [[1, 2, 3, 4]]
assert list(filled_windows([1,2,3,4,5], 5)) == [[1, 2, 3, 4, 5]]
assert list(filled_windows([1,2,3,4,5], 6)) == []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment