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

[js] 排列组合矩阵实现 #21

Open
VaJoy opened this issue Sep 9, 2016 · 4 comments
Open

[js] 排列组合矩阵实现 #21

VaJoy opened this issue Sep 9, 2016 · 4 comments

Comments

@VaJoy
Copy link
Member

VaJoy commented Sep 9, 2016

有这么几个变量:

  • a 其值可能为 0、1
  • b 其值可能为 3、4、5
  • c 其值可能为 6、7

实现一个函数 getArranging 可以返回它们的可能值的排列组合二维数组:

function getArranging( ){
      //TODO - 返回参数的排列组合
}

var a = [0, 1];
var b = [3, 4, 5];
var c = [6, 7];

var ret = getArranging( a, b, c );
// ret 值为:
/**
[ [0,3,6], [0,3,7], [0,4,6], [0,4,7], [0,5,6], [0,5,7], [1,3,6], [1,3,7], [1,4,6], [1,4,7], [1,5,6], [1,5,7] ]
**/
@blade254353074
Copy link

function cartesian (...lists) {
  return lists
    .map(list => list.map(item => [item]))
    .reduce((listsA, listsB) =>
      listsA.reduce((list, listA) =>
        list.concat(
          listsB.map(listB => listA.concat(listB))
        ), []
      )
    )
}

@Jiasm
Copy link
Member

Jiasm commented Apr 14, 2018

(() => {
  function getArranging (...list) {
    return build(list)
  }

  function build (list, result = list.shift()) {
    return !list.length ? result : build(list, [].concat(...list.shift().map(item =>
      result.map(resultItem => 
        [].concat(item, resultItem)
      )
    )))
  }

  let a = [0, 1]
  let b = [3, 4, 5]
  let c = [6, 7]

  let ret = getArranging( a, b, c )

  console.log(JSON.stringify(ret))
})()

@uinz
Copy link

uinz commented Jul 25, 2018

function getArranging(...args: number[][]) {
    const result = [];

    (function loop(tmp: number[] = []) {
        if (tmp.length === args.length) {
            result.push(tmp)
            return
        }
        const values = args[tmp.length]
        values.forEach(value => {
            loop([...tmp, value])
        })
    })();

    return result;

}

console.log(
    getArranging([0, 1], [3, 4, 5], [6, 7])
);

@lshnltt
Copy link

lshnltt commented Jan 8, 2019

function getArranging(...list){
//TODO - 返回参数的排列组合
return list.reduce((result, curr, listIndex) => {
curr.forEach((item, index) => {
let insertCount = len(list)/curr.length;
let interval = len(list.slice(listIndex));
let sufixLen = len(list.slice(listIndex + 1));
let startPosition = index * sufixLen;
Array.from(new Array(insertCount)).forEach((_item, _index) => {
let round = (Math.ceil((_index + 1) / sufixLen) - 1) * interval;
position = startPosition + ((_index % sufixLen == 0 || !sufixLen) ? round : round + _index % sufixLen);
result[position] = result[position] || [];
result[position][listIndex] = item;
});
});
return result;
}, []);
}
function len(list) {
return list.reduce((result, curr) => {
result *= curr.length;
return result;
}, 1);
}
var a = [0, 1];
var b = [3, 4, 5];
var c = [6, 7];
var d = [8, 9, 10]
var ret = getArranging( a, b, c, d );
console.log(ret);

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

5 participants