javascript permutation generator with permutation length parameter

后端 未结 4 1060
鱼传尺愫
鱼传尺愫 2020-12-05 22:45

I\'ve seen a few generators out there but they all make a squared matrix. For example, you give it a list of three items and it\'ll assume the output of the length is also t

4条回答
  •  一生所求
    2020-12-05 23:12

    I wrote a little library that uses generators to give you permutations with custom items and number of elements. https://github.com/acarl005/generatorics

    const G = require('generatorics')
    
    for (let perm of G.permutation(['a', 'b', 'c'], 2)) {
      console.log(perm);
    }
    // [ 'a', 'b' ]
    // [ 'a', 'c' ]
    // [ 'b', 'a' ]
    // [ 'b', 'c' ]
    // [ 'c', 'a' ]
    // [ 'c', 'b' ]
    

提交回复
热议问题