]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Remove itertool recipe with low pedagogical value (gh-113138)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Thu, 14 Dec 2023 20:36:40 +0000 (14:36 -0600)
committerGitHub <noreply@github.com>
Thu, 14 Dec 2023 20:36:40 +0000 (14:36 -0600)
Doc/library/itertools.rst

index 83e2a9fdb7b4643d890f080284e63e1bb25ec44c..36cea9a835f30247cff3a56ab9ea8500e6ea9856 100644 (file)
@@ -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