summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2023-12-14 20:36:40 (GMT)
committerGitHub <noreply@github.com>2023-12-14 20:36:40 (GMT)
commitbecad9a2a1b5f3deaad24759daec95014218e0db (patch)
treeaf636ab453acc794349d4e1b594700b1a5a13ab9 /Doc
parent006355b2a966060541166608ecfec4c957b85f55 (diff)
downloadcpython-becad9a2a1b5f3deaad24759daec95014218e0db.zip
cpython-becad9a2a1b5f3deaad24759daec95014218e0db.tar.gz
cpython-becad9a2a1b5f3deaad24759daec95014218e0db.tar.bz2
Remove itertool recipe with low pedagogical value (gh-113138)
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/itertools.rst64
1 files changed, 32 insertions, 32 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 83e2a9f..36cea9a 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -1136,24 +1136,6 @@ The following recipes have a more mathematical flavor:
n = n // p * (p - 1)
return n
- def nth_combination(iterable, r, index):
- "Equivalent to list(combinations(iterable, r))[index]"
- pool = tuple(iterable)
- n = len(pool)
- c = math.comb(n, r)
- if index < 0:
- index += c
- if index < 0 or index >= c:
- raise IndexError
- result = []
- while r:
- c, n, r = c*r//n, n-1, r-1
- while index >= c:
- index -= c
- c, n = c*(n-r)//n, n-1
- result.append(pool[-1-n])
- return tuple(result)
-
.. doctest::
:hide:
@@ -1577,20 +1559,6 @@ The following recipes have a more mathematical flavor:
>>> first_true('ABC0DEF1', '9', str.isdigit)
'0'
- >>> population = 'ABCDEFGH'
- >>> for r in range(len(population) + 1):
- ... seq = list(combinations(population, r))
- ... for i in range(len(seq)):
- ... assert nth_combination(population, r, i) == seq[i]
- ... for i in range(-len(seq), 0):
- ... assert nth_combination(population, r, i) == seq[i]
-
- >>> iterable = 'abcde'
- >>> r = 3
- >>> combos = list(combinations(iterable, r))
- >>> all(nth_combination(iterable, r, i) == comb for i, comb in enumerate(combos))
- True
-
.. testcode::
:hide:
@@ -1617,6 +1585,24 @@ The following recipes have a more mathematical flavor:
for (a, _), (b, c) in pairwise(pairwise(iterable)):
yield a, b, c
+ def nth_combination(iterable, r, index):
+ "Equivalent to list(combinations(iterable, r))[index]"
+ pool = tuple(iterable)
+ n = len(pool)
+ c = math.comb(n, r)
+ if index < 0:
+ index += c
+ if index < 0 or index >= c:
+ raise IndexError
+ result = []
+ while r:
+ c, n, r = c*r//n, n-1, r-1
+ while index >= c:
+ index -= c
+ c, n = c*(n-r)//n, n-1
+ result.append(pool[-1-n])
+ return tuple(result)
+
.. doctest::
:hide:
@@ -1632,3 +1618,17 @@ The following recipes have a more mathematical flavor:
>>> list(triplewise('ABCDEFG'))
[('A', 'B', 'C'), ('B', 'C', 'D'), ('C', 'D', 'E'), ('D', 'E', 'F'), ('E', 'F', 'G')]
+
+ >>> population = 'ABCDEFGH'
+ >>> for r in range(len(population) + 1):
+ ... seq = list(combinations(population, r))
+ ... for i in range(len(seq)):
+ ... assert nth_combination(population, r, i) == seq[i]
+ ... for i in range(-len(seq), 0):
+ ... assert nth_combination(population, r, i) == seq[i]
+
+ >>> iterable = 'abcde'
+ >>> r = 3
+ >>> combos = list(combinations(iterable, r))
+ >>> all(nth_combination(iterable, r, i) == comb for i, comb in enumerate(combos))
+ True