permutation ( n seq -- seq' ) (original) (raw)

Vocabulary
math.combinatorics

Inputs

n a non-negative integer
seq a sequence

Outputs

seq' a sequence

Word description
Outputs the nth lexicographical permutation of seq.

Notes
Permutations are 0-based and a bounds error will be thrown if n is larger than seq length factorial 1 -.

Examples

USING: math.combinatorics prettyprint ; 1 { 0 1 2 } permutation .
{ 0 2 1 }

USING: math.combinatorics prettyprint ; 5 { "apple" "banana" "orange" } permutation .
{ "orange" "banana" "apple" }

Definition

USING: kernel math.combinatorics.private ;

IN: math.combinatorics

: permutation ( n seq -- seq' )
[ permutation-indices ] keep nths-unsafe ;