Permutations in JavaScript?

前端 未结 30 2683
不思量自难忘°
不思量自难忘° 2020-11-21 06:52

I\'m trying to write a function that does the following:

  • takes an array of integers as an argument (e.g. [1,2,3,4])
  • creates an array of all the possib
30条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-21 07:29

    Most of the other answers do not utilize the new javascript generator functions which is a perfect solution to this type of problem. You probably only need one permutation at time in memory. Also, I prefer to generate a permutation of a range of indices as this allows me to index each permutation and jump straight to any particular permutation as well as be used to permutate any other collection.

    // ES6 generator version of python itertools [permutations and combinations]
    const range = function*(l) { for (let i = 0; i < l; i+=1) yield i; }
    const isEmpty = arr => arr.length === 0;
    
    const permutations = function*(a) {
        const r = arguments[1] || [];
        if (isEmpty(a)) yield r;
        for (let i of range(a.length)) {
            const aa = [...a];
            const rr = [...r, ...aa.splice(i, 1)];
            yield* permutations(aa, rr);
        }
    }
    console.log('permutations of ABC');
    console.log(JSON.stringify([...permutations([...'ABC'])]));
    
    const combinations = function*(a, count) {
        const r = arguments[2] || [];
        if (count) {
            count = count - 1;
            for (let i of range(a.length - count)) {
                const aa = a.slice(i);
                const rr = [...r, ...aa.splice(0, 1)];
                yield* combinations(aa, count, rr);
            }
        } else {
            yield r;
        }
    }
    console.log('combinations of 2 of ABC');
    console.log(JSON.stringify([...combinations([...'ABC'], 2)]));
    
    
    
    const permutator = function() {
        const range = function*(args) {
            let {begin = 0, count} = args;
            for (let i = begin; count; count--, i+=1) {
                yield i;
            }
        }
        const factorial = fact => fact ? fact * factorial(fact - 1) : 1;
    
        return {
            perm: function(n, permutationId) {
                const indexCount = factorial(n);
                permutationId = ((permutationId%indexCount)+indexCount)%indexCount;
    
                let permutation = [0];
                for (const choiceCount of range({begin: 2, count: n-1})) {
                    const choice = permutationId % choiceCount;
                    const lastIndex = permutation.length;
    
                    permutation.push(choice);
                    permutation = permutation.map((cv, i, orig) => 
                        (cv < choice || i == lastIndex) ? cv : cv + 1
                    );
    
                    permutationId = Math.floor(permutationId / choiceCount);
                }
                return permutation.reverse();
            },
            perms: function*(n) {
                for (let i of range({count: factorial(n)})) {
                    yield this.perm(n, i);
                }
            }
        };
    }();
    
    console.log('indexing type permutator');
    let i = 0;
    for (let elem of permutator.perms(3)) {
      console.log(`${i}: ${elem}`);
      i+=1;
    }
    console.log();
    console.log(`3: ${permutator.perm(3,3)}`);

提交回复
热议问题