Skip to content

Commit 59719a2

Browse files
authored
Take advantage of math.comb() in the nth_combination() recipe (#93027)
1 parent d59b2d0 commit 59719a2

File tree

1 file changed

+2
-6
lines changed

1 file changed

+2
-6
lines changed

Doc/library/itertools.rst

+2-6
Original file line numberDiff line numberDiff line change
@@ -992,12 +992,7 @@ which incur interpreter overhead.
992992
"Equivalent to list(combinations(iterable, r))[index]"
993993
pool = tuple(iterable)
994994
n = len(pool)
995-
if r < 0 or r > n:
996-
raise ValueError
997-
c = 1
998-
k = min(r, n-r)
999-
for i in range(1, k+1):
1000-
c = c * (n - k + i) // i
995+
c = math.comb(n, r)
1001996
if index < 0:
1002997
index += c
1003998
if index < 0 or index >= c:
@@ -1071,6 +1066,7 @@ which incur interpreter overhead.
10711066

10721067
>>> import operator
10731068
>>> import collections
1069+
>>> import math
10741070

10751071
>>> take(10, count())
10761072
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

0 commit comments

Comments
 (0)