aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-05-20 16:34:30 -0700
committerGitHub <noreply@github.com>2022-05-20 18:34:30 -0500
commitb52e44d9330b6a02c4215b664cd92e7562242635 (patch)
tree6949f404ffc4d9af8bc6abb8b0286265b148a8e0
parent[3.11] gh-72073: Add Windows case in pathlib.rename (GH-93002) (GH-93028) (diff)
downloadcpython-b52e44d9330b6a02c4215b664cd92e7562242635.tar.gz
cpython-b52e44d9330b6a02c4215b664cd92e7562242635.tar.bz2
cpython-b52e44d9330b6a02c4215b664cd92e7562242635.zip
Take advantage of math.comb() in the nth_combination() recipe (GH-93027) (#93032)
-rw-r--r--Doc/library/itertools.rst8
1 files changed, 2 insertions, 6 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index 26c9253cb7f..416c4eca5eb 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -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]