aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/more-itertools/py3/more_itertools/recipes.py
diff options
context:
space:
mode:
authorrobot-piglet <robot-piglet@yandex-team.com>2024-08-22 12:08:26 +0300
committerrobot-piglet <robot-piglet@yandex-team.com>2024-08-22 12:17:43 +0300
commit9b7b894a85081d8b88c312d5797e86e3166cea7d (patch)
treebfca0f422a9649a31bab0e9b2d1ab2ae37f5f9e7 /contrib/python/more-itertools/py3/more_itertools/recipes.py
parenta04076863119aed8a4fd5d4b31b42f83283a2d8d (diff)
downloadydb-9b7b894a85081d8b88c312d5797e86e3166cea7d.tar.gz
Intermediate changes
Diffstat (limited to 'contrib/python/more-itertools/py3/more_itertools/recipes.py')
-rw-r--r--contrib/python/more-itertools/py3/more_itertools/recipes.py48
1 files changed, 36 insertions, 12 deletions
diff --git a/contrib/python/more-itertools/py3/more_itertools/recipes.py b/contrib/python/more-itertools/py3/more_itertools/recipes.py
index b32fa95533..a21a1f5d88 100644
--- a/contrib/python/more-itertools/py3/more_itertools/recipes.py
+++ b/contrib/python/more-itertools/py3/more_itertools/recipes.py
@@ -795,8 +795,30 @@ def triplewise(iterable):
[('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E')]
"""
- for (a, _), (b, c) in pairwise(pairwise(iterable)):
- yield a, b, c
+ # This deviates from the itertools documentation reciple - see
+ # https://github.com/more-itertools/more-itertools/issues/889
+ t1, t2, t3 = tee(iterable, 3)
+ next(t3, None)
+ next(t3, None)
+ next(t2, None)
+ return zip(t1, t2, t3)
+
+
+def _sliding_window_islice(iterable, n):
+ # Fast path for small, non-zero values of n.
+ iterators = tee(iterable, n)
+ for i, iterator in enumerate(iterators):
+ next(islice(iterator, i, i), None)
+ return zip(*iterators)
+
+
+def _sliding_window_deque(iterable, n):
+ # Normal path for other values of n.
+ it = iter(iterable)
+ window = deque(islice(it, n - 1), maxlen=n)
+ for x in it:
+ window.append(x)
+ yield tuple(window)
def sliding_window(iterable, n):
@@ -812,11 +834,16 @@ def sliding_window(iterable, n):
For a variant with more features, see :func:`windowed`.
"""
- it = iter(iterable)
- window = deque(islice(it, n - 1), maxlen=n)
- for x in it:
- window.append(x)
- yield tuple(window)
+ if n > 20:
+ return _sliding_window_deque(iterable, n)
+ elif n > 2:
+ return _sliding_window_islice(iterable, n)
+ elif n == 2:
+ return pairwise(iterable)
+ elif n == 1:
+ return zip(iterable)
+ else:
+ raise ValueError(f'n should be at least one, not {n}')
def subslices(iterable):
@@ -1038,9 +1065,6 @@ def totient(n):
>>> totient(12)
4
"""
- # The itertools docs use unique_justseen instead of set; see
- # https://github.com/more-itertools/more-itertools/issues/823
- for p in set(factor(n)):
- n = n // p * (p - 1)
-
+ for prime in set(factor(n)):
+ n -= n // prime
return n