recipes.py 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  1. """Imported from the recipes section of the itertools documentation.
  2. All functions taken from the recipes section of the itertools library docs
  3. [1]_.
  4. Some backward-compatible usability improvements have been made.
  5. .. [1] http://docs.python.org/library/itertools.html#recipes
  6. """
  7. import math
  8. import operator
  9. from collections import deque
  10. from collections.abc import Sized
  11. from functools import lru_cache, partial
  12. from itertools import (
  13. chain,
  14. combinations,
  15. compress,
  16. count,
  17. cycle,
  18. groupby,
  19. islice,
  20. product,
  21. repeat,
  22. starmap,
  23. tee,
  24. zip_longest,
  25. )
  26. from random import randrange, sample, choice
  27. from sys import hexversion
  28. __all__ = [
  29. 'all_equal',
  30. 'batched',
  31. 'before_and_after',
  32. 'consume',
  33. 'convolve',
  34. 'dotproduct',
  35. 'first_true',
  36. 'factor',
  37. 'flatten',
  38. 'grouper',
  39. 'is_prime',
  40. 'iter_except',
  41. 'iter_index',
  42. 'loops',
  43. 'matmul',
  44. 'ncycles',
  45. 'nth',
  46. 'nth_combination',
  47. 'padnone',
  48. 'pad_none',
  49. 'pairwise',
  50. 'partition',
  51. 'polynomial_eval',
  52. 'polynomial_from_roots',
  53. 'polynomial_derivative',
  54. 'powerset',
  55. 'prepend',
  56. 'quantify',
  57. 'reshape',
  58. 'random_combination_with_replacement',
  59. 'random_combination',
  60. 'random_permutation',
  61. 'random_product',
  62. 'repeatfunc',
  63. 'roundrobin',
  64. 'sieve',
  65. 'sliding_window',
  66. 'subslices',
  67. 'sum_of_squares',
  68. 'tabulate',
  69. 'tail',
  70. 'take',
  71. 'totient',
  72. 'transpose',
  73. 'triplewise',
  74. 'unique',
  75. 'unique_everseen',
  76. 'unique_justseen',
  77. ]
  78. _marker = object()
  79. # zip with strict is available for Python 3.10+
  80. try:
  81. zip(strict=True)
  82. except TypeError:
  83. _zip_strict = zip
  84. else:
  85. _zip_strict = partial(zip, strict=True)
  86. # math.sumprod is available for Python 3.12+
  87. _sumprod = getattr(math, 'sumprod', lambda x, y: dotproduct(x, y))
  88. def take(n, iterable):
  89. """Return first *n* items of the iterable as a list.
  90. >>> take(3, range(10))
  91. [0, 1, 2]
  92. If there are fewer than *n* items in the iterable, all of them are
  93. returned.
  94. >>> take(10, range(3))
  95. [0, 1, 2]
  96. """
  97. return list(islice(iterable, n))
  98. def tabulate(function, start=0):
  99. """Return an iterator over the results of ``func(start)``,
  100. ``func(start + 1)``, ``func(start + 2)``...
  101. *func* should be a function that accepts one integer argument.
  102. If *start* is not specified it defaults to 0. It will be incremented each
  103. time the iterator is advanced.
  104. >>> square = lambda x: x ** 2
  105. >>> iterator = tabulate(square, -3)
  106. >>> take(4, iterator)
  107. [9, 4, 1, 0]
  108. """
  109. return map(function, count(start))
  110. def tail(n, iterable):
  111. """Return an iterator over the last *n* items of *iterable*.
  112. >>> t = tail(3, 'ABCDEFG')
  113. >>> list(t)
  114. ['E', 'F', 'G']
  115. """
  116. # If the given iterable has a length, then we can use islice to get its
  117. # final elements. Note that if the iterable is not actually Iterable,
  118. # either islice or deque will throw a TypeError. This is why we don't
  119. # check if it is Iterable.
  120. if isinstance(iterable, Sized):
  121. yield from islice(iterable, max(0, len(iterable) - n), None)
  122. else:
  123. yield from iter(deque(iterable, maxlen=n))
  124. def consume(iterator, n=None):
  125. """Advance *iterable* by *n* steps. If *n* is ``None``, consume it
  126. entirely.
  127. Efficiently exhausts an iterator without returning values. Defaults to
  128. consuming the whole iterator, but an optional second argument may be
  129. provided to limit consumption.
  130. >>> i = (x for x in range(10))
  131. >>> next(i)
  132. 0
  133. >>> consume(i, 3)
  134. >>> next(i)
  135. 4
  136. >>> consume(i)
  137. >>> next(i)
  138. Traceback (most recent call last):
  139. File "<stdin>", line 1, in <module>
  140. StopIteration
  141. If the iterator has fewer items remaining than the provided limit, the
  142. whole iterator will be consumed.
  143. >>> i = (x for x in range(3))
  144. >>> consume(i, 5)
  145. >>> next(i)
  146. Traceback (most recent call last):
  147. File "<stdin>", line 1, in <module>
  148. StopIteration
  149. """
  150. # Use functions that consume iterators at C speed.
  151. if n is None:
  152. # feed the entire iterator into a zero-length deque
  153. deque(iterator, maxlen=0)
  154. else:
  155. # advance to the empty slice starting at position n
  156. next(islice(iterator, n, n), None)
  157. def nth(iterable, n, default=None):
  158. """Returns the nth item or a default value.
  159. >>> l = range(10)
  160. >>> nth(l, 3)
  161. 3
  162. >>> nth(l, 20, "zebra")
  163. 'zebra'
  164. """
  165. return next(islice(iterable, n, None), default)
  166. def all_equal(iterable, key=None):
  167. """
  168. Returns ``True`` if all the elements are equal to each other.
  169. >>> all_equal('aaaa')
  170. True
  171. >>> all_equal('aaab')
  172. False
  173. A function that accepts a single argument and returns a transformed version
  174. of each input item can be specified with *key*:
  175. >>> all_equal('AaaA', key=str.casefold)
  176. True
  177. >>> all_equal([1, 2, 3], key=lambda x: x < 10)
  178. True
  179. """
  180. iterator = groupby(iterable, key)
  181. for first in iterator:
  182. for second in iterator:
  183. return False
  184. return True
  185. return True
  186. def quantify(iterable, pred=bool):
  187. """Return the how many times the predicate is true.
  188. >>> quantify([True, False, True])
  189. 2
  190. """
  191. return sum(map(pred, iterable))
  192. def pad_none(iterable):
  193. """Returns the sequence of elements and then returns ``None`` indefinitely.
  194. >>> take(5, pad_none(range(3)))
  195. [0, 1, 2, None, None]
  196. Useful for emulating the behavior of the built-in :func:`map` function.
  197. See also :func:`padded`.
  198. """
  199. return chain(iterable, repeat(None))
  200. padnone = pad_none
  201. def ncycles(iterable, n):
  202. """Returns the sequence elements *n* times
  203. >>> list(ncycles(["a", "b"], 3))
  204. ['a', 'b', 'a', 'b', 'a', 'b']
  205. """
  206. return chain.from_iterable(repeat(tuple(iterable), n))
  207. def dotproduct(vec1, vec2):
  208. """Returns the dot product of the two iterables.
  209. >>> dotproduct([10, 10], [20, 20])
  210. 400
  211. """
  212. return sum(map(operator.mul, vec1, vec2))
  213. def flatten(listOfLists):
  214. """Return an iterator flattening one level of nesting in a list of lists.
  215. >>> list(flatten([[0, 1], [2, 3]]))
  216. [0, 1, 2, 3]
  217. See also :func:`collapse`, which can flatten multiple levels of nesting.
  218. """
  219. return chain.from_iterable(listOfLists)
  220. def repeatfunc(func, times=None, *args):
  221. """Call *func* with *args* repeatedly, returning an iterable over the
  222. results.
  223. If *times* is specified, the iterable will terminate after that many
  224. repetitions:
  225. >>> from operator import add
  226. >>> times = 4
  227. >>> args = 3, 5
  228. >>> list(repeatfunc(add, times, *args))
  229. [8, 8, 8, 8]
  230. If *times* is ``None`` the iterable will not terminate:
  231. >>> from random import randrange
  232. >>> times = None
  233. >>> args = 1, 11
  234. >>> take(6, repeatfunc(randrange, times, *args)) # doctest:+SKIP
  235. [2, 4, 8, 1, 8, 4]
  236. """
  237. if times is None:
  238. return starmap(func, repeat(args))
  239. return starmap(func, repeat(args, times))
  240. def _pairwise(iterable):
  241. """Returns an iterator of paired items, overlapping, from the original
  242. >>> take(4, pairwise(count()))
  243. [(0, 1), (1, 2), (2, 3), (3, 4)]
  244. On Python 3.10 and above, this is an alias for :func:`itertools.pairwise`.
  245. """
  246. a, b = tee(iterable)
  247. next(b, None)
  248. return zip(a, b)
  249. try:
  250. from itertools import pairwise as itertools_pairwise
  251. except ImportError:
  252. pairwise = _pairwise
  253. else:
  254. def pairwise(iterable):
  255. return itertools_pairwise(iterable)
  256. pairwise.__doc__ = _pairwise.__doc__
  257. class UnequalIterablesError(ValueError):
  258. def __init__(self, details=None):
  259. msg = 'Iterables have different lengths'
  260. if details is not None:
  261. msg += (': index 0 has length {}; index {} has length {}').format(
  262. *details
  263. )
  264. super().__init__(msg)
  265. def _zip_equal_generator(iterables):
  266. for combo in zip_longest(*iterables, fillvalue=_marker):
  267. for val in combo:
  268. if val is _marker:
  269. raise UnequalIterablesError()
  270. yield combo
  271. def _zip_equal(*iterables):
  272. # Check whether the iterables are all the same size.
  273. try:
  274. first_size = len(iterables[0])
  275. for i, it in enumerate(iterables[1:], 1):
  276. size = len(it)
  277. if size != first_size:
  278. raise UnequalIterablesError(details=(first_size, i, size))
  279. # All sizes are equal, we can use the built-in zip.
  280. return zip(*iterables)
  281. # If any one of the iterables didn't have a length, start reading
  282. # them until one runs out.
  283. except TypeError:
  284. return _zip_equal_generator(iterables)
  285. def grouper(iterable, n, incomplete='fill', fillvalue=None):
  286. """Group elements from *iterable* into fixed-length groups of length *n*.
  287. >>> list(grouper('ABCDEF', 3))
  288. [('A', 'B', 'C'), ('D', 'E', 'F')]
  289. The keyword arguments *incomplete* and *fillvalue* control what happens for
  290. iterables whose length is not a multiple of *n*.
  291. When *incomplete* is `'fill'`, the last group will contain instances of
  292. *fillvalue*.
  293. >>> list(grouper('ABCDEFG', 3, incomplete='fill', fillvalue='x'))
  294. [('A', 'B', 'C'), ('D', 'E', 'F'), ('G', 'x', 'x')]
  295. When *incomplete* is `'ignore'`, the last group will not be emitted.
  296. >>> list(grouper('ABCDEFG', 3, incomplete='ignore', fillvalue='x'))
  297. [('A', 'B', 'C'), ('D', 'E', 'F')]
  298. When *incomplete* is `'strict'`, a subclass of `ValueError` will be raised.
  299. >>> it = grouper('ABCDEFG', 3, incomplete='strict')
  300. >>> list(it) # doctest: +IGNORE_EXCEPTION_DETAIL
  301. Traceback (most recent call last):
  302. ...
  303. UnequalIterablesError
  304. """
  305. args = [iter(iterable)] * n
  306. if incomplete == 'fill':
  307. return zip_longest(*args, fillvalue=fillvalue)
  308. if incomplete == 'strict':
  309. return _zip_equal(*args)
  310. if incomplete == 'ignore':
  311. return zip(*args)
  312. else:
  313. raise ValueError('Expected fill, strict, or ignore')
  314. def roundrobin(*iterables):
  315. """Yields an item from each iterable, alternating between them.
  316. >>> list(roundrobin('ABC', 'D', 'EF'))
  317. ['A', 'D', 'E', 'B', 'F', 'C']
  318. This function produces the same output as :func:`interleave_longest`, but
  319. may perform better for some inputs (in particular when the number of
  320. iterables is small).
  321. """
  322. # Algorithm credited to George Sakkis
  323. iterators = map(iter, iterables)
  324. for num_active in range(len(iterables), 0, -1):
  325. iterators = cycle(islice(iterators, num_active))
  326. yield from map(next, iterators)
  327. def partition(pred, iterable):
  328. """
  329. Returns a 2-tuple of iterables derived from the input iterable.
  330. The first yields the items that have ``pred(item) == False``.
  331. The second yields the items that have ``pred(item) == True``.
  332. >>> is_odd = lambda x: x % 2 != 0
  333. >>> iterable = range(10)
  334. >>> even_items, odd_items = partition(is_odd, iterable)
  335. >>> list(even_items), list(odd_items)
  336. ([0, 2, 4, 6, 8], [1, 3, 5, 7, 9])
  337. If *pred* is None, :func:`bool` is used.
  338. >>> iterable = [0, 1, False, True, '', ' ']
  339. >>> false_items, true_items = partition(None, iterable)
  340. >>> list(false_items), list(true_items)
  341. ([0, False, ''], [1, True, ' '])
  342. """
  343. if pred is None:
  344. pred = bool
  345. t1, t2, p = tee(iterable, 3)
  346. p1, p2 = tee(map(pred, p))
  347. return (compress(t1, map(operator.not_, p1)), compress(t2, p2))
  348. def powerset(iterable):
  349. """Yields all possible subsets of the iterable.
  350. >>> list(powerset([1, 2, 3]))
  351. [(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
  352. :func:`powerset` will operate on iterables that aren't :class:`set`
  353. instances, so repeated elements in the input will produce repeated elements
  354. in the output.
  355. >>> seq = [1, 1, 0]
  356. >>> list(powerset(seq))
  357. [(), (1,), (1,), (0,), (1, 1), (1, 0), (1, 0), (1, 1, 0)]
  358. For a variant that efficiently yields actual :class:`set` instances, see
  359. :func:`powerset_of_sets`.
  360. """
  361. s = list(iterable)
  362. return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))
  363. def unique_everseen(iterable, key=None):
  364. """
  365. Yield unique elements, preserving order.
  366. >>> list(unique_everseen('AAAABBBCCDAABBB'))
  367. ['A', 'B', 'C', 'D']
  368. >>> list(unique_everseen('ABBCcAD', str.lower))
  369. ['A', 'B', 'C', 'D']
  370. Sequences with a mix of hashable and unhashable items can be used.
  371. The function will be slower (i.e., `O(n^2)`) for unhashable items.
  372. Remember that ``list`` objects are unhashable - you can use the *key*
  373. parameter to transform the list to a tuple (which is hashable) to
  374. avoid a slowdown.
  375. >>> iterable = ([1, 2], [2, 3], [1, 2])
  376. >>> list(unique_everseen(iterable)) # Slow
  377. [[1, 2], [2, 3]]
  378. >>> list(unique_everseen(iterable, key=tuple)) # Faster
  379. [[1, 2], [2, 3]]
  380. Similarly, you may want to convert unhashable ``set`` objects with
  381. ``key=frozenset``. For ``dict`` objects,
  382. ``key=lambda x: frozenset(x.items())`` can be used.
  383. """
  384. seenset = set()
  385. seenset_add = seenset.add
  386. seenlist = []
  387. seenlist_add = seenlist.append
  388. use_key = key is not None
  389. for element in iterable:
  390. k = key(element) if use_key else element
  391. try:
  392. if k not in seenset:
  393. seenset_add(k)
  394. yield element
  395. except TypeError:
  396. if k not in seenlist:
  397. seenlist_add(k)
  398. yield element
  399. def unique_justseen(iterable, key=None):
  400. """Yields elements in order, ignoring serial duplicates
  401. >>> list(unique_justseen('AAAABBBCCDAABBB'))
  402. ['A', 'B', 'C', 'D', 'A', 'B']
  403. >>> list(unique_justseen('ABBCcAD', str.lower))
  404. ['A', 'B', 'C', 'A', 'D']
  405. """
  406. if key is None:
  407. return map(operator.itemgetter(0), groupby(iterable))
  408. return map(next, map(operator.itemgetter(1), groupby(iterable, key)))
  409. def unique(iterable, key=None, reverse=False):
  410. """Yields unique elements in sorted order.
  411. >>> list(unique([[1, 2], [3, 4], [1, 2]]))
  412. [[1, 2], [3, 4]]
  413. *key* and *reverse* are passed to :func:`sorted`.
  414. >>> list(unique('ABBcCAD', str.casefold))
  415. ['A', 'B', 'c', 'D']
  416. >>> list(unique('ABBcCAD', str.casefold, reverse=True))
  417. ['D', 'c', 'B', 'A']
  418. The elements in *iterable* need not be hashable, but they must be
  419. comparable for sorting to work.
  420. """
  421. return unique_justseen(sorted(iterable, key=key, reverse=reverse), key=key)
  422. def iter_except(func, exception, first=None):
  423. """Yields results from a function repeatedly until an exception is raised.
  424. Converts a call-until-exception interface to an iterator interface.
  425. Like ``iter(func, sentinel)``, but uses an exception instead of a sentinel
  426. to end the loop.
  427. >>> l = [0, 1, 2]
  428. >>> list(iter_except(l.pop, IndexError))
  429. [2, 1, 0]
  430. Multiple exceptions can be specified as a stopping condition:
  431. >>> l = [1, 2, 3, '...', 4, 5, 6]
  432. >>> list(iter_except(lambda: 1 + l.pop(), (IndexError, TypeError)))
  433. [7, 6, 5]
  434. >>> list(iter_except(lambda: 1 + l.pop(), (IndexError, TypeError)))
  435. [4, 3, 2]
  436. >>> list(iter_except(lambda: 1 + l.pop(), (IndexError, TypeError)))
  437. []
  438. """
  439. try:
  440. if first is not None:
  441. yield first()
  442. while 1:
  443. yield func()
  444. except exception:
  445. pass
  446. def first_true(iterable, default=None, pred=None):
  447. """
  448. Returns the first true value in the iterable.
  449. If no true value is found, returns *default*
  450. If *pred* is not None, returns the first item for which
  451. ``pred(item) == True`` .
  452. >>> first_true(range(10))
  453. 1
  454. >>> first_true(range(10), pred=lambda x: x > 5)
  455. 6
  456. >>> first_true(range(10), default='missing', pred=lambda x: x > 9)
  457. 'missing'
  458. """
  459. return next(filter(pred, iterable), default)
  460. def random_product(*args, repeat=1):
  461. """Draw an item at random from each of the input iterables.
  462. >>> random_product('abc', range(4), 'XYZ') # doctest:+SKIP
  463. ('c', 3, 'Z')
  464. If *repeat* is provided as a keyword argument, that many items will be
  465. drawn from each iterable.
  466. >>> random_product('abcd', range(4), repeat=2) # doctest:+SKIP
  467. ('a', 2, 'd', 3)
  468. This equivalent to taking a random selection from
  469. ``itertools.product(*args, **kwarg)``.
  470. """
  471. pools = [tuple(pool) for pool in args] * repeat
  472. return tuple(choice(pool) for pool in pools)
  473. def random_permutation(iterable, r=None):
  474. """Return a random *r* length permutation of the elements in *iterable*.
  475. If *r* is not specified or is ``None``, then *r* defaults to the length of
  476. *iterable*.
  477. >>> random_permutation(range(5)) # doctest:+SKIP
  478. (3, 4, 0, 1, 2)
  479. This equivalent to taking a random selection from
  480. ``itertools.permutations(iterable, r)``.
  481. """
  482. pool = tuple(iterable)
  483. r = len(pool) if r is None else r
  484. return tuple(sample(pool, r))
  485. def random_combination(iterable, r):
  486. """Return a random *r* length subsequence of the elements in *iterable*.
  487. >>> random_combination(range(5), 3) # doctest:+SKIP
  488. (2, 3, 4)
  489. This equivalent to taking a random selection from
  490. ``itertools.combinations(iterable, r)``.
  491. """
  492. pool = tuple(iterable)
  493. n = len(pool)
  494. indices = sorted(sample(range(n), r))
  495. return tuple(pool[i] for i in indices)
  496. def random_combination_with_replacement(iterable, r):
  497. """Return a random *r* length subsequence of elements in *iterable*,
  498. allowing individual elements to be repeated.
  499. >>> random_combination_with_replacement(range(3), 5) # doctest:+SKIP
  500. (0, 0, 1, 2, 2)
  501. This equivalent to taking a random selection from
  502. ``itertools.combinations_with_replacement(iterable, r)``.
  503. """
  504. pool = tuple(iterable)
  505. n = len(pool)
  506. indices = sorted(randrange(n) for i in range(r))
  507. return tuple(pool[i] for i in indices)
  508. def nth_combination(iterable, r, index):
  509. """Equivalent to ``list(combinations(iterable, r))[index]``.
  510. The subsequences of *iterable* that are of length *r* can be ordered
  511. lexicographically. :func:`nth_combination` computes the subsequence at
  512. sort position *index* directly, without computing the previous
  513. subsequences.
  514. >>> nth_combination(range(5), 3, 5)
  515. (0, 3, 4)
  516. ``ValueError`` will be raised If *r* is negative or greater than the length
  517. of *iterable*.
  518. ``IndexError`` will be raised if the given *index* is invalid.
  519. """
  520. pool = tuple(iterable)
  521. n = len(pool)
  522. if (r < 0) or (r > n):
  523. raise ValueError
  524. c = 1
  525. k = min(r, n - r)
  526. for i in range(1, k + 1):
  527. c = c * (n - k + i) // i
  528. if index < 0:
  529. index += c
  530. if (index < 0) or (index >= c):
  531. raise IndexError
  532. result = []
  533. while r:
  534. c, n, r = c * r // n, n - 1, r - 1
  535. while index >= c:
  536. index -= c
  537. c, n = c * (n - r) // n, n - 1
  538. result.append(pool[-1 - n])
  539. return tuple(result)
  540. def prepend(value, iterator):
  541. """Yield *value*, followed by the elements in *iterator*.
  542. >>> value = '0'
  543. >>> iterator = ['1', '2', '3']
  544. >>> list(prepend(value, iterator))
  545. ['0', '1', '2', '3']
  546. To prepend multiple values, see :func:`itertools.chain`
  547. or :func:`value_chain`.
  548. """
  549. return chain([value], iterator)
  550. def convolve(signal, kernel):
  551. """Convolve the iterable *signal* with the iterable *kernel*.
  552. >>> signal = (1, 2, 3, 4, 5)
  553. >>> kernel = [3, 2, 1]
  554. >>> list(convolve(signal, kernel))
  555. [3, 8, 14, 20, 26, 14, 5]
  556. Note: the input arguments are not interchangeable, as the *kernel*
  557. is immediately consumed and stored.
  558. """
  559. # This implementation intentionally doesn't match the one in the itertools
  560. # documentation.
  561. kernel = tuple(kernel)[::-1]
  562. n = len(kernel)
  563. window = deque([0], maxlen=n) * n
  564. for x in chain(signal, repeat(0, n - 1)):
  565. window.append(x)
  566. yield _sumprod(kernel, window)
  567. def before_and_after(predicate, it):
  568. """A variant of :func:`takewhile` that allows complete access to the
  569. remainder of the iterator.
  570. >>> it = iter('ABCdEfGhI')
  571. >>> all_upper, remainder = before_and_after(str.isupper, it)
  572. >>> ''.join(all_upper)
  573. 'ABC'
  574. >>> ''.join(remainder) # takewhile() would lose the 'd'
  575. 'dEfGhI'
  576. Note that the first iterator must be fully consumed before the second
  577. iterator can generate valid results.
  578. """
  579. it = iter(it)
  580. transition = []
  581. def true_iterator():
  582. for elem in it:
  583. if predicate(elem):
  584. yield elem
  585. else:
  586. transition.append(elem)
  587. return
  588. # Note: this is different from itertools recipes to allow nesting
  589. # before_and_after remainders into before_and_after again. See tests
  590. # for an example.
  591. remainder_iterator = chain(transition, it)
  592. return true_iterator(), remainder_iterator
  593. def triplewise(iterable):
  594. """Return overlapping triplets from *iterable*.
  595. >>> list(triplewise('ABCDE'))
  596. [('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E')]
  597. """
  598. # This deviates from the itertools documentation reciple - see
  599. # https://github.com/more-itertools/more-itertools/issues/889
  600. t1, t2, t3 = tee(iterable, 3)
  601. next(t3, None)
  602. next(t3, None)
  603. next(t2, None)
  604. return zip(t1, t2, t3)
  605. def _sliding_window_islice(iterable, n):
  606. # Fast path for small, non-zero values of n.
  607. iterators = tee(iterable, n)
  608. for i, iterator in enumerate(iterators):
  609. next(islice(iterator, i, i), None)
  610. return zip(*iterators)
  611. def _sliding_window_deque(iterable, n):
  612. # Normal path for other values of n.
  613. it = iter(iterable)
  614. window = deque(islice(it, n - 1), maxlen=n)
  615. for x in it:
  616. window.append(x)
  617. yield tuple(window)
  618. def sliding_window(iterable, n):
  619. """Return a sliding window of width *n* over *iterable*.
  620. >>> list(sliding_window(range(6), 4))
  621. [(0, 1, 2, 3), (1, 2, 3, 4), (2, 3, 4, 5)]
  622. If *iterable* has fewer than *n* items, then nothing is yielded:
  623. >>> list(sliding_window(range(3), 4))
  624. []
  625. For a variant with more features, see :func:`windowed`.
  626. """
  627. if n > 20:
  628. return _sliding_window_deque(iterable, n)
  629. elif n > 2:
  630. return _sliding_window_islice(iterable, n)
  631. elif n == 2:
  632. return pairwise(iterable)
  633. elif n == 1:
  634. return zip(iterable)
  635. else:
  636. raise ValueError(f'n should be at least one, not {n}')
  637. def subslices(iterable):
  638. """Return all contiguous non-empty subslices of *iterable*.
  639. >>> list(subslices('ABC'))
  640. [['A'], ['A', 'B'], ['A', 'B', 'C'], ['B'], ['B', 'C'], ['C']]
  641. This is similar to :func:`substrings`, but emits items in a different
  642. order.
  643. """
  644. seq = list(iterable)
  645. slices = starmap(slice, combinations(range(len(seq) + 1), 2))
  646. return map(operator.getitem, repeat(seq), slices)
  647. def polynomial_from_roots(roots):
  648. """Compute a polynomial's coefficients from its roots.
  649. >>> roots = [5, -4, 3] # (x - 5) * (x + 4) * (x - 3)
  650. >>> polynomial_from_roots(roots) # x^3 - 4 * x^2 - 17 * x + 60
  651. [1, -4, -17, 60]
  652. """
  653. poly = [1]
  654. for root in roots:
  655. poly = list(convolve(poly, (1, -root)))
  656. return poly
  657. def iter_index(iterable, value, start=0, stop=None):
  658. """Yield the index of each place in *iterable* that *value* occurs,
  659. beginning with index *start* and ending before index *stop*.
  660. >>> list(iter_index('AABCADEAF', 'A'))
  661. [0, 1, 4, 7]
  662. >>> list(iter_index('AABCADEAF', 'A', 1)) # start index is inclusive
  663. [1, 4, 7]
  664. >>> list(iter_index('AABCADEAF', 'A', 1, 7)) # stop index is not inclusive
  665. [1, 4]
  666. The behavior for non-scalar *values* matches the built-in Python types.
  667. >>> list(iter_index('ABCDABCD', 'AB'))
  668. [0, 4]
  669. >>> list(iter_index([0, 1, 2, 3, 0, 1, 2, 3], [0, 1]))
  670. []
  671. >>> list(iter_index([[0, 1], [2, 3], [0, 1], [2, 3]], [0, 1]))
  672. [0, 2]
  673. See :func:`locate` for a more general means of finding the indexes
  674. associated with particular values.
  675. """
  676. seq_index = getattr(iterable, 'index', None)
  677. if seq_index is None:
  678. # Slow path for general iterables
  679. it = islice(iterable, start, stop)
  680. for i, element in enumerate(it, start):
  681. if element is value or element == value:
  682. yield i
  683. else:
  684. # Fast path for sequences
  685. stop = len(iterable) if stop is None else stop
  686. i = start - 1
  687. try:
  688. while True:
  689. yield (i := seq_index(value, i + 1, stop))
  690. except ValueError:
  691. pass
  692. def sieve(n):
  693. """Yield the primes less than n.
  694. >>> list(sieve(30))
  695. [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
  696. """
  697. if n > 2:
  698. yield 2
  699. start = 3
  700. data = bytearray((0, 1)) * (n // 2)
  701. limit = math.isqrt(n) + 1
  702. for p in iter_index(data, 1, start, limit):
  703. yield from iter_index(data, 1, start, p * p)
  704. data[p * p : n : p + p] = bytes(len(range(p * p, n, p + p)))
  705. start = p * p
  706. yield from iter_index(data, 1, start)
  707. def _batched(iterable, n, *, strict=False):
  708. """Batch data into tuples of length *n*. If the number of items in
  709. *iterable* is not divisible by *n*:
  710. * The last batch will be shorter if *strict* is ``False``.
  711. * :exc:`ValueError` will be raised if *strict* is ``True``.
  712. >>> list(batched('ABCDEFG', 3))
  713. [('A', 'B', 'C'), ('D', 'E', 'F'), ('G',)]
  714. On Python 3.13 and above, this is an alias for :func:`itertools.batched`.
  715. """
  716. if n < 1:
  717. raise ValueError('n must be at least one')
  718. it = iter(iterable)
  719. while batch := tuple(islice(it, n)):
  720. if strict and len(batch) != n:
  721. raise ValueError('batched(): incomplete batch')
  722. yield batch
  723. if hexversion >= 0x30D00A2:
  724. from itertools import batched as itertools_batched
  725. def batched(iterable, n, *, strict=False):
  726. return itertools_batched(iterable, n, strict=strict)
  727. else:
  728. batched = _batched
  729. batched.__doc__ = _batched.__doc__
  730. def transpose(it):
  731. """Swap the rows and columns of the input matrix.
  732. >>> list(transpose([(1, 2, 3), (11, 22, 33)]))
  733. [(1, 11), (2, 22), (3, 33)]
  734. The caller should ensure that the dimensions of the input are compatible.
  735. If the input is empty, no output will be produced.
  736. """
  737. return _zip_strict(*it)
  738. def reshape(matrix, cols):
  739. """Reshape the 2-D input *matrix* to have a column count given by *cols*.
  740. >>> matrix = [(0, 1), (2, 3), (4, 5)]
  741. >>> cols = 3
  742. >>> list(reshape(matrix, cols))
  743. [(0, 1, 2), (3, 4, 5)]
  744. """
  745. return batched(chain.from_iterable(matrix), cols)
  746. def matmul(m1, m2):
  747. """Multiply two matrices.
  748. >>> list(matmul([(7, 5), (3, 5)], [(2, 5), (7, 9)]))
  749. [(49, 80), (41, 60)]
  750. The caller should ensure that the dimensions of the input matrices are
  751. compatible with each other.
  752. """
  753. n = len(m2[0])
  754. return batched(starmap(_sumprod, product(m1, transpose(m2))), n)
  755. def _factor_pollard(n):
  756. # Return a factor of n using Pollard's rho algorithm
  757. gcd = math.gcd
  758. for b in range(1, n - 2):
  759. x = y = 2
  760. d = 1
  761. while d == 1:
  762. x = (x * x + b) % n
  763. y = (y * y + b) % n
  764. y = (y * y + b) % n
  765. d = gcd(x - y, n)
  766. if d != n:
  767. return d
  768. raise ValueError('prime or under 5')
  769. _primes_below_211 = tuple(sieve(211))
  770. def factor(n):
  771. """Yield the prime factors of n.
  772. >>> list(factor(360))
  773. [2, 2, 2, 3, 3, 5]
  774. Finds small factors with trial division. Larger factors are
  775. either verified as prime with ``is_prime`` or split into
  776. smaller factors with Pollard's rho algorithm.
  777. """
  778. # Corner case reduction
  779. if n < 2:
  780. return
  781. # Trial division reduction
  782. for prime in _primes_below_211:
  783. while not n % prime:
  784. yield prime
  785. n //= prime
  786. # Pollard's rho reduction
  787. primes = []
  788. todo = [n] if n > 1 else []
  789. for n in todo:
  790. if n < 211**2 or is_prime(n):
  791. primes.append(n)
  792. else:
  793. fact = _factor_pollard(n)
  794. todo += (fact, n // fact)
  795. yield from sorted(primes)
  796. def polynomial_eval(coefficients, x):
  797. """Evaluate a polynomial at a specific value.
  798. Example: evaluating x^3 - 4 * x^2 - 17 * x + 60 at x = 2.5:
  799. >>> coefficients = [1, -4, -17, 60]
  800. >>> x = 2.5
  801. >>> polynomial_eval(coefficients, x)
  802. 8.125
  803. """
  804. n = len(coefficients)
  805. if n == 0:
  806. return x * 0 # coerce zero to the type of x
  807. powers = map(pow, repeat(x), reversed(range(n)))
  808. return _sumprod(coefficients, powers)
  809. def sum_of_squares(it):
  810. """Return the sum of the squares of the input values.
  811. >>> sum_of_squares([10, 20, 30])
  812. 1400
  813. """
  814. return _sumprod(*tee(it))
  815. def polynomial_derivative(coefficients):
  816. """Compute the first derivative of a polynomial.
  817. Example: evaluating the derivative of x^3 - 4 * x^2 - 17 * x + 60
  818. >>> coefficients = [1, -4, -17, 60]
  819. >>> derivative_coefficients = polynomial_derivative(coefficients)
  820. >>> derivative_coefficients
  821. [3, -8, -17]
  822. """
  823. n = len(coefficients)
  824. powers = reversed(range(1, n))
  825. return list(map(operator.mul, coefficients, powers))
  826. def totient(n):
  827. """Return the count of natural numbers up to *n* that are coprime with *n*.
  828. >>> totient(9)
  829. 6
  830. >>> totient(12)
  831. 4
  832. """
  833. for prime in set(factor(n)):
  834. n -= n // prime
  835. return n
  836. # Miller–Rabin primality test: https://oeis.org/A014233
  837. _perfect_tests = [
  838. (2047, (2,)),
  839. (9080191, (31, 73)),
  840. (4759123141, (2, 7, 61)),
  841. (1122004669633, (2, 13, 23, 1662803)),
  842. (2152302898747, (2, 3, 5, 7, 11)),
  843. (3474749660383, (2, 3, 5, 7, 11, 13)),
  844. (18446744073709551616, (2, 325, 9375, 28178, 450775, 9780504, 1795265022)),
  845. (
  846. 3317044064679887385961981,
  847. (2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41),
  848. ),
  849. ]
  850. @lru_cache
  851. def _shift_to_odd(n):
  852. 'Return s, d such that 2**s * d == n'
  853. s = ((n - 1) ^ n).bit_length() - 1
  854. d = n >> s
  855. assert (1 << s) * d == n and d & 1 and s >= 0
  856. return s, d
  857. def _strong_probable_prime(n, base):
  858. assert (n > 2) and (n & 1) and (2 <= base < n)
  859. s, d = _shift_to_odd(n - 1)
  860. x = pow(base, d, n)
  861. if x == 1 or x == n - 1:
  862. return True
  863. for _ in range(s - 1):
  864. x = x * x % n
  865. if x == n - 1:
  866. return True
  867. return False
  868. def is_prime(n):
  869. """Return ``True`` if *n* is prime and ``False`` otherwise.
  870. >>> is_prime(37)
  871. True
  872. >>> is_prime(3 * 13)
  873. False
  874. >>> is_prime(18_446_744_073_709_551_557)
  875. True
  876. This function uses the Miller-Rabin primality test, which can return false
  877. positives for very large inputs. For values of *n* below 10**24
  878. there are no false positives. For larger values, there is less than
  879. a 1 in 2**128 false positive rate. Multiple tests can further reduce the
  880. chance of a false positive.
  881. """
  882. if n < 17:
  883. return n in {2, 3, 5, 7, 11, 13}
  884. if not (n & 1 and n % 3 and n % 5 and n % 7 and n % 11 and n % 13):
  885. return False
  886. for limit, bases in _perfect_tests:
  887. if n < limit:
  888. break
  889. else:
  890. bases = [randrange(2, n - 1) for i in range(64)]
  891. return all(_strong_probable_prime(n, base) for base in bases)
  892. def loops(n):
  893. """Returns an iterable with *n* elements for efficient looping.
  894. Like ``range(n)`` but doesn't create integers.
  895. >>> i = 0
  896. >>> for _ in loops(5):
  897. ... i += 1
  898. >>> i
  899. 5
  900. """
  901. return repeat(None, n)