Expand/Shrink

permute

Definition: sequence res = permute(integer n, sequence set)
Description: Return the nth permute of the given set.
n should be an integer in the range 1 to factorial(length(set))
Comments: This can be used to retrieve all possible permutations, in no particular order. The elements can be any type. It is just as fast to generate the (n!)th permutation as the first, so some applications may benefit by storing an integer key rather than duplicating all the elements of the given set.
Example:
for i=1 to factorial(3) do
    ?permute(i,"abc")       -- displays "bca" "cab" "bac" "cba" "acb" "abc"
end for
Implementation: See builtins\permute.e (an autoinclude), reproduced in the technicalia dropdown below, for details of the actual implementation.
See Also: factorial
Expand/Shrink