236
  8   ˆl–Ðz¾”Šj"Tâ‚À½q¶”Å£�þco,ãe#qº•à?Ì„i·[ËÀÉÈ ?÷     """Imported from the recipes section of the itertools documentation.

All functions taken from the recipes section of the itertools library docs
[1]_.
Some backward-compatible usability improvements have been made.

.. [1] http://docs.python.org/library/itertools.html#recipes

"""

import random

from bisect import bisect_left, insort
from collections import deque
from contextlib import suppress
from dataclasses import dataclass
from functools import lru_cache, reduce
from heapq import heappush, heappushpop
from itertools import (
    accumulate,
    chain,
    combinations,
    compress,
    count,
    cycle,
    filterfalse,
    groupby,
    islice,
    pairwise as itertools_pairwise,
    product,
    repeat,
    starmap,
    takewhile,
    tee,
    zip_longest,
)
from math import prod, comb, isqrt, gcd
from operator import mul, getitem, index as _index, is_, itemgetter, truediv
from random import randrange, sample, choice, shuffle
from sys import hexversion

__all__ = [
    'Stats',
    'all_equal',
    'batched',
    'before_and_after',
    'consume',
    'convolve',
    'dotproduct',
    'first_true',
    'factor',
    'flatten',
    'grouper',
    'is_prime',
    'iter_except',
    'iter_index',
    'loops',
    'matmul',
    'multinomial',
    'ncycles',
    'nth',
    'nth_combination',
    'padnone',
    'pad_none',
    'pairwise',
    'partition',
    'polynomial_eval',
    'polynomial_from_roots',
    'polynomial_derivative',
    'powerset',
    'prepend',
    'quantify',
    'reshape',
    'random_combination_with_replacement',
    'random_combination',
    'random_derangement',
    'random_permutation',
    'random_product',
    'repeatfunc',
    'roundrobin',
    'running_max',
    'running_mean',
    'running_median',
    'running_min',
    'running_statistics',
    'sieve',
    'sliding_window',
    'subslices',
    'sum_of_squares',
    'tabulate',
    'tail',
    'take',
    'totient',
    'transpose',
    'triplewise',
    'unique',
    'unique_everseen',
    'unique_justseen',
]

_marker = object()


# heapq max-heap functions are available for Python 3.14+
try:
    from heapq import heappush_max, heappushpop_max
except ImportError:  # pragma: no cover
    _max_heap_available = False
else:  # pragma: no cover
    _max_heap_available = True


def take(n, iterable):
    """Return first *n* items of the *iterable* as a list.

        >>> take(3, range(10))
        [0, 1, 2]

    If there are fewer than *n* items in the iterable, all of them are
    returned.

        >>> take(10, range(3))
        [0, 1, 2]

    """
    return list(islice(iterable, n))


def tabulate(function, start=0):
    """Return an iterator over the results of ``func(start)``,
    ``func(start + 1)``, ``func(start + 2)``...

    *func* should be a function that accepts one integer argument.

    If *start* is not specified it defaults to 0. It will be incremented each
    time the iterator is advanced.

        >>> square = lambda x: x ** 2
        >>> iterator = tabulate(square, -3)
        >>> take(4, iterator)
        [9, 4, 1, 0]

    """
    return map(function, count(start))


def tail(n, iterable):
    """Return an iterator over the last *n* items of *iterable*.

    >>> t = tail(3, 'ABCDEFG')
    >>> list(t)
    ['E', 'F', 'G']

    """
    try:
        size = len(iterable)
    except TypeError:
        return iter(deque(iterable, maxlen=n))
    else:
        return islice(iterable, max(0, size - n), None)


def consume(iterator, n=None):
    """Advance *iterable* by *n* steps. If *n* is ``None``, consume it
    entirely.

    Efficiently exhausts an iterator without returning values. Defaults to
    consuming the whole iterator, but an optional second argument may be
    provided to limit consumption.

        >>> i = (x for x in range(10))
        >>> next(i)
        0
        >>> consume(i, 3)
        >>> next(i)
        4
        >>> consume(i)
        >>> next(i)
        Traceback (most recent call last):
          File "<stdin>", line 1, in <