deno.land / x / simplestatistic@v7.7.1 / src / combinations.js

combinations.js
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/** * Implementation of Combinations * Combinations are unique subsets of a collection - in this case, k x from a collection at a time. * https://en.wikipedia.org/wiki/Combination * @param {Array} x any type of data * @param {int} k the number of objects in each group (without replacement) * @returns {Array<Array>} array of permutations * @example * combinations([1, 2, 3], 2); // => [[1,2], [1,3], [2,3]] */
function combinations(x, k) { let i; let subI; const combinationList = []; let subsetCombinations; let next;
for (i = 0; i < x.length; i++) { if (k === 1) { combinationList.push([x[i]]); } else { subsetCombinations = combinations(x.slice(i + 1, x.length), k - 1); for (subI = 0; subI < subsetCombinations.length; subI++) { next = subsetCombinations[subI]; next.unshift(x[i]); combinationList.push(next); } } } return combinationList;}
export default combinations;
simplestatistic

Version Info

Tagged at
2 years ago