【问题标题】:How to get combination of array with specific sequence?如何获得具有特定序列的数组组合?
【发布时间】:2018-09-18 11:13:54
【问题描述】:
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',
]

【问题讨论】:

  • @LucaKiebel 这不是您分享的那个(链接)的重复
  • 你能解释一下你对这个特定序列的期望吗? a c 是如何在 a b c 之后出现的?

标签: javascript arrays combinations


【解决方案1】:

您需要的是所有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);

【讨论】:

  • 聪明的兄弟......我已经被困了一天......你刚刚解决了它。阿里加托
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多