some code compaction

This commit is contained in:
2021-12-02 09:46:21 +01:00
parent 62275676a3
commit cfeb794e91
4 changed files with 16 additions and 17 deletions

26
libs/sliding.py Normal file
View File

@@ -0,0 +1,26 @@
def sliding(iterable,window=1):
# pairwise('ABCDEFG') --> AB BC CD DE EF FG
from itertools import tee
a, b = tee(iterable)
IT = [ ]
for _ in range(window):
next(b, None)
return zip(a, b)
def sliding_window(iterable, n):
from collections import deque
from itertools import islice
# sliding_window('ABCDEFG', 4) -> ABCD BCDE CDEF DEFG
it = iter(iterable)
window = deque(islice(it, n), maxlen=n)
#window = deque(it, maxlen=n)
if len(window) == n:
yield tuple(window)
for x in it:
window.append(x)
yield tuple(window)
import sys
print([t for t in sliding_window(map(lambda x: int(x[:-1]), sys.stdin.readlines()),4)])
#print(a for a in sliding(map(lambda x: int(x[:-1]), sys.stdin.readlines()),3))