函数返回阵列组合阵列 [英] Function that returns array of array combinations
本文介绍了函数返回阵列组合阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图做一个 _。组合
函数(下划线混入)的三个参数改编,口袋,复制
。这里有一个测试,我设计展现的行为应该如何。
I'm trying to make a _.combinations
function (underscore mixin) that takes three parameters arr, pockets, duplicates
. Here's a test that I designed to show how the behavior should be.
expect(_.combinations([1, 2], 1, false)).to.be.equal([[1],[2]])
expect(_.combinations([1, 2], 1, true)).to.be.equal([[1],[2]])
expect(_.combinations([1, 2, 3], 2, false)).to.be.equal([[1,2],[1,3],[2,3]])
expect(_.combinations([1, 2, 3], 2, true)).to.be.equal([[1,2],[1,3],[2,3],[2,1],[3,1],[3,2]])
expect(_.combinations([1, 2, 3, 4], 3, false)).to.be.equal([[1,2,3],[1,2,4],[1,3,4],[2,1,4],[2,3,4],[3,4,1]])
expect(_.combinations([1, 2, 3, 4], 3, true)).to.be.equal([[1,2,3],[1,2,4],[1,3,4],[2,1,4],[2,3,1],[2,3,4],[3,1,2],[3,4,1],[3,4,2],[4,1,2],[4,1,3],[4,2,3]])
我在想,在我走之前,如果一个库中已经存在创建此功能。也许这特定的功能已经具备,我不是熟悉的一个名字。
I was wondering before I go and create this function if it existed within a library already. Perhaps this specific function already has a name that I'm not familiar with.
有什么在那里,这是否?
Is there something out there that does this?
推荐答案
这图书馆有很好的作用。我认为它的pretty多少得到了你所需要的。
This library has good function. I think its pretty much got what you need.
var combinatorics=require('/path/to/combinatorics');
var a = [1,2,3];
var ans1=combinatorics.permutation(a,2);
console.log(ans1.toArray());// [[1,2],[2,1],[1,3],[3,1],[2,3],[3,2]] like when duplicates is set to true
var ans2=combinatorics.combination(a,2);
console.log(ans2.toArray());//[[1,2],[2,1],[1,3],[3,1],[2,3],[3,2]] like when duplicates is set to false
这篇关于函数返回阵列组合阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文