]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Permutations Python code equivalent in comment was invalid for Python 3 (GH-16927)
authorSergey <bizywiz@gmail.com>
Tue, 29 Oct 2019 05:10:24 +0000 (08:10 +0300)
committerRaymond Hettinger <rhettinger@users.noreply.github.com>
Tue, 29 Oct 2019 05:10:24 +0000 (22:10 -0700)
Modules/itertoolsmodule.c

index e60ad5bec43c6bd7a91115c1b54524f3780f4f64..3d39fa2e4737bd4fa52ad4013ed8ba2d786d3d74 100644 (file)
@@ -3076,12 +3076,15 @@ static PyTypeObject cwr_type = {
 /* permutations object ********************************************************
 
 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)
     n = len(pool)
     r = n if r is None else r
-    indices = range(n)
-    cycles = range(n-r+1, n+1)[::-1]
+    if r > n:
+        return
+    indices = list(range(n))
+    cycles = list(range(n, n-r, -1))
     yield tuple(pool[i] for i in indices[:r])
     while n:
         for i in reversed(range(r)):