如何获得具有特定序列的数组的组合?

时间:2018-09-18 11:13:55

标签: javascript arrays combinations

var combiArray =  ["a", "b", "c"];

var result = [];

for(var i =0 ; i<combiArray.length;i++){
    result.push(combiArray[i])
    for(var b =0 ; b<combiArray.length;b++){
        if(i!=b){
            result.push(combiArray[i]+" "+combiArray[b])
        }

    }
}

//MY OUTPUT: 
[ 'a', 'a b', 'a c', 'b', 'b a', 'b c', 'c', 'c a', 'c b' ]

//WHAT I WANT IS THIS SEQUENCE
[
'a',
'a b',
'a b c',
'a c',
'a c b',
'b',
'b a',    
'b a c',
'b c',    
'b c a',    
'c',
'c a',
'c a b',
'c b',
'c b a',
]

1 个答案:

答案 0 :(得分:2)

您需要为所有combinations

进行排列

这是用于创建所有组合的代码:

function combination(arr) {
let res= [], len = Math.pow(2, arr.length), c, b, com;
    for(c=1;c<len;c++) {
        b = c.toString(2).padStart(arr.length, "0").split("");
        com = arr.filter((_, idx)=>b[idx]==="1");
        res.push(com.join(" "));
    }
    return res;
    }

实际行动

function combination(arr) {
let res= [], len = Math.pow(2, arr.length), c, b, com;
for(c=1;c<len;c++) {
	b = c.toString(2).padStart(arr.length, "0").split("");
	com = arr.filter((_, idx)=>b[idx]==="1");
	res.push(com.join(" "));
}
return res;
}

console.log(combination(["a", "b", "c"]));

console.log(combination(["a", "b", "c", "d"]));

现在,您需要为每个组合进行排列。例如,对于特定的组合a b c,您需要找到["abc", "cba", "acb", ....]

所以让我们创建一个排列代码:

此置换代码摘自this stackoverflow question

function permutation(input) {
var permArr = [],
  usedChars = [];

function permute(input) {
  var i, ch;
  for (i = 0; i < input.length; i++) {
    ch = input.splice(i, 1)[0];
    usedChars.push(ch);
    if (input.length == 0) {
      permArr.push(usedChars.slice());
    }
    permute(input);
    input.splice(i, 0, ch);
    usedChars.pop();
  }
  return permArr
}

return permute(input);
}

实际操作:

function permutation (input) {
    var permArr = [],
      usedChars = [];
    
    function permute(input) {
      var i, ch;
      for (i = 0; i < input.length; i++) {
        ch = input.splice(i, 1)[0];
        usedChars.push(ch);
        if (input.length == 0) {
          permArr.push(usedChars.slice());
        }
        permute(input);
        input.splice(i, 0, ch);
        usedChars.pop();
      }
      return permArr
    }
    
    return permute(input);
    }
    
    console.log(permutation (["a", "b"]));
    console.log(permutation (["a", "b", "c"]));

现在,您需要对它们进行组合,以便获得期望输出以对所有组合进行排列。一旦我们拥有了所有想要的输出,我们便可以查看序列。下面的示例中添加了样本排序。

function permutation(input) {
  var permArr = [],
    usedChars = [];

  function permute(input) {
    var i, ch;
    for (i = 0; i < input.length; i++) {
      ch = input.splice(i, 1)[0];
      usedChars.push(ch);
      if (input.length == 0) {
        permArr.push(usedChars.slice());
      }
      permute(input);
      input.splice(i, 0, ch);
      usedChars.pop();
    }
    return permArr
  }

  return permute(input);
}

function allCombos(arr) {
let res= [], len = Math.pow(2, arr.length), c, b, com, per;

for(c=1;c<len;c++) {
	b = c.toString(2).padStart(arr.length, "0").split("");
	com = arr.filter((_, idx)=>b[idx]==="1");
	per = permutation(com).map(e=>e.join(" "));
	res.push(...per);
}
return res;
}

var res = allCombos(["a", "b", "c"]);

console.log(res);

//Now, we get all the outputs we want. for the sequence it's seems like its
//an alphabetical sequence, or its a sequencne in order they appears in the input collection.
//Let's just sort alphabetically, if any other sort is required that can be easily done in the final output.
var finalRes = res.sort((a,b)=> a.localeCompare(b));
console.log('Final resule: ', finalRes);

相关问题