diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-01 08:29:46 +0000 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2021-12-01 10:18:48 +0000 |
commit | 1f88313cc4ff52f06b5c99ff2364910d8bc10e1d (patch) | |
tree | 3fc0bf35e2bed217dbbbe9fc6de7a61132bafe1a /utils.py | |
download | aoc2021-1f88313cc4ff52f06b5c99ff2364910d8bc10e1d.tar.gz aoc2021-1f88313cc4ff52f06b5c99ff2364910d8bc10e1d.tar.xz aoc2021-1f88313cc4ff52f06b5c99ff2364910d8bc10e1d.zip |
day 1
Diffstat (limited to 'utils.py')
-rw-r--r-- | utils.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/utils.py b/utils.py new file mode 100644 index 0000000..d322ed0 --- /dev/null +++ b/utils.py @@ -0,0 +1,15 @@ +from collections import deque +from collections.abc import Iterable, Iterator, Generator +from itertools import islice +from typing import TypeVar + +T = TypeVar('T') +def sliding_window(iterable: Iterable[T], n: int) -> Generator[tuple[T, ...], None, None]: + # sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG + it: Iterator[T] = iter(iterable) + window: deque[T] = deque(islice(it, n), maxlen=n) + if len(window) == n: + yield tuple(window) + for x in it: + window.append(x) + yield tuple(window) |