Created
June 13, 2021 19:07
-
-
Save EkremDincel/4bdd2a144341b1919dc301b9a0d57037 to your computer and use it in GitHub Desktop.
This file contains 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 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