Permutations Python code equivalent in comment was invalid for Python 3 (GH-16927)

This commit is contained in:
Sergey 2019-10-29 08:10:24 +03:00 committed by Raymond Hettinger
parent 3c88199e0b
commit 0078a0c2a5
1 changed files with 6 additions and 3 deletions

View File

@ -3076,12 +3076,15 @@ static PyTypeObject cwr_type = {
/* permutations object ******************************************************** /* permutations object ********************************************************
def permutations(iterable, r=None): def permutations(iterable, r=None):
'permutations(range(3), 2) --> (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)' # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
# permutations(range(3)) --> 012 021 102 120 201 210
pool = tuple(iterable) pool = tuple(iterable)
n = len(pool) n = len(pool)
r = n if r is None else r r = n if r is None else r
indices = range(n) if r > n:
cycles = range(n-r+1, n+1)[::-1] return
indices = list(range(n))
cycles = list(range(n, n-r, -1))
yield tuple(pool[i] for i in indices[:r]) yield tuple(pool[i] for i in indices[:r])
while n: while n:
for i in reversed(range(r)): for i in reversed(range(r)):