Permutations: Difference between revisions

Content added Content deleted
(→‎{{header|Python}}: python implementation, recursive)
Line 2,989: Line 2,989:


These two solutions make use of a generator, and "yield from" introduced in [https://www.python.org/dev/peps/pep-0380/ PEP-380]. They are slightly different: the latter produces permutations in lexicographic order, because the "remaining" part of a (that is, a[i:]) is always sorted, whereas the former always reverses the exchange just after the recursive call.
These two solutions make use of a generator, and "yield from" introduced in [https://www.python.org/dev/peps/pep-0380/ PEP-380]. They are slightly different: the latter produces permutations in lexicographic order, because the "remaining" part of a (that is, a[i:]) is always sorted, whereas the former always reverses the exchange just after the recursive call.

On three elements, the difference can be seen on the last two permutations:


<lang python>for u in perm1(3): print(u)
<lang python>for u in perm1(3): print(u)