Skip to content

Commit 0078a0c

Browse files
bizywizyrhettinger
authored andcommitted
Permutations Python code equivalent in comment was invalid for Python 3 (pythonGH-16927)
1 parent 3c88199 commit 0078a0c

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Modules/itertoolsmodule.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3076,12 +3076,15 @@ static PyTypeObject cwr_type = {
30763076
/* permutations object ********************************************************
30773077
30783078
def permutations(iterable, r=None):
3079-
'permutations(range(3), 2) --> (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)'
3079+
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
3080+
# permutations(range(3)) --> 012 021 102 120 201 210
30803081
pool = tuple(iterable)
30813082
n = len(pool)
30823083
r = n if r is None else r
3083-
indices = range(n)
3084-
cycles = range(n-r+1, n+1)[::-1]
3084+
if r > n:
3085+
return
3086+
indices = list(range(n))
3087+
cycles = list(range(n, n-r, -1))
30853088
yield tuple(pool[i] for i in indices[:r])
30863089
while n:
30873090
for i in reversed(range(r)):

0 commit comments

Comments
 (0)