]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Take advantage of math.comb() in the nth_combination() recipe (#93027)
authorRaymond Hettinger <rhettinger@users.noreply.github.com>
Fri, 20 May 2022 23:02:33 +0000 (18:02 -0500)
committerGitHub <noreply@github.com>
Fri, 20 May 2022 23:02:33 +0000 (18:02 -0500)
Doc/library/itertools.rst

index 26c9253cb7f5f617c80adedd84c2a1aba5c0c43d..416c4eca5eb48a9657c8f4619b31d964ddd57c7a 100644 (file)
@@ -992,12 +992,7 @@ which incur interpreter overhead.
        "Equivalent to list(combinations(iterable, r))[index]"
        pool = tuple(iterable)
        n = len(pool)
-       if r < 0 or r > n:
-           raise ValueError
-       c = 1
-       k = min(r, n-r)
-       for i in range(1, k+1):
-           c = c * (n - k + i) // i
+       c = math.comb(n, r)
        if index < 0:
            index += c
        if index < 0 or index >= c:
@@ -1071,6 +1066,7 @@ which incur interpreter overhead.
 
     >>> import operator
     >>> import collections
+    >>> import math
 
     >>> take(10, count())
     [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]