Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a method to get the N-th lexicographical permutation #46

Open
Etheryte opened this issue May 11, 2018 · 1 comment
Open

Add a method to get the N-th lexicographical permutation #46

Etheryte opened this issue May 11, 2018 · 1 comment

Comments

@Etheryte
Copy link
Contributor

In some cases, it's favorable to get a single permutation instead of finding all the permutations, especially for large N.

I've written a sample implementation based on this article:

// Input: Number
// Output: Array of the factoradic representation digits of the input
function factoradic(n) {
    var f = [];
    for (var i = 1; n > 0; n = Math.floor(n / i++)) {
        f.push(n % i);
    }
    return f.reverse();
}

// Input: Array, Number
// Output: N-th lexographical permutation of the input array
function nthPermutation(a, n) {
    var p = [];
    var fac = factoradic(n);
    var copy = a.slice();
    for (var i = 0; i < fac.length; i++) {
        p.push(copy.splice(fac[i], 1)[0]);
    }
    return p;
}
@ETHproductions
Copy link
Owner

Great suggestion. Thanks to Jelly, I've wanted to do this for a while, so thanks for putting together an implementation. A small bug: when n is less than factorial(a.length - 1), the output doesn't contain all items of the input. The best way I think we can fix that is by adding a second argument to the factoradic function that determines the length of the permutation. So:

// Input: Number, Number
// Output: Array of the factoradic representation digits of the input
function factoradic(n, l) {
    var f = [];
    for (var i = 1; l > 0; l--) {
        f.push(n % i);
        n = Math.floor(n / i++);
    }
    return f.reverse();
}

// Input: Array, Number
// Output: N-th lexographical permutation of the input array
function nthPermutation(a, n) {
    var p = [];
    var fac = factoradic(n, a.length);
    var copy = a.slice();
    for (var i = 0; i < fac.length; i++) {
        p.push(copy.splice(fac[i], 1)[0]);
    }
    return p;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants