Find all combinations of two arrays
Solution for any parameter (as long as the result is countable):
Edit: This version avoids possible problems with len = Math.pow(left.length, right.length)
and problems with length over 36 (cnr!).
Example:
combine(['A', 'B'], [1, 2, 3])
all possible rows are 2^3 = 8. In this example, the distribution is binary, but it changes the base with more parameters ofleft
.
distribution included in set
i c A B
----------------------------------------
0 0 0 0 { 1, 2, 3 } { }
1 0 0 1 { 1, 2 } { 3 }
2 0 1 0 { 1, 3 } { 2 }
3 0 1 1 { 1 } { 2, 3 }
4 1 0 0 { 2, 3 } { 1 }
5 1 0 1 { 2 } { 1, 3 }
6 1 1 0 { 3 } { 1, 2 }
7 1 1 1 { } { 1, 2, 3 }
The distribution
i = 3
of0 1 1
evaluates as:
- Take the first
0
and take it as index of leftleft[0] = A
and move the place value of 0 of rightright[0] = 1
to set A.- Take the second
1
and take it as index of leftleft[1] = B
and move the place value of 1 of rightright[1] = 2
to set B.- Take the third
1
and take it as index of leftleft[1] = B
and move the place value of 2 of rightright[2] = 3
to set B.
Another example:
combine(['A', 'B', 'C'], [1, 2])
all possible rows are 3^2 = 9.
distribution included in set
i c A B C
----------------------------------------
0 0 0 { 1, 2 } { } { }
1 0 1 { 1 } { 2 } { }
2 0 2 { 1 } { } { 2 }
3 1 0 { 2 } { 1 } { }
4 1 1 { } { 1, 2 } { }
5 1 2 { } { 1 } { 2 }
6 2 0 { 2 } { } { 1 }
7 2 1 { } { 2 } { 1 }
8 2 2 { } { } { 1, 2 }
function combine(left, right) {
function carry() {
return c.reduceRight(function (r, _, i, o) {
return r && !(o[i] = (o[i] + 1) % left.length);
}, 1);
}
var c = Array.apply(null, { length: right.length }).map(function () { return 0; }),
result = [];
do {
result.push(c.reduce(function (r, a, i) {
r[left[a]].push(right[i]);
return r;
}, left.reduce(function (r, a) {
r[a] = [];
return r;
}, {})));
} while (!carry());
return result;
}
document.getElementById('out').innerHTML =
"combine(['A', 'B'], [1, 2, 3]) = " + JSON.stringify(combine(['A', 'B'], [1, 2, 3]), null, 4) + '\n'+
"combine(['A', 'B', 'C'], [1, 2] = " + JSON.stringify(combine(['A', 'B', 'C'], [1, 2]), null, 4) +'\n'+
"combine(['A', 'B', 'C'], [1, 2, 3] = " + JSON.stringify(combine(['A', 'B', 'C'], [1, 2, 3]), null, 4) +'\n'+
"combine(['A', 'B', 'C', 'D'], [1, 2, 3, 4, 5, 6]) = " + JSON.stringify(combine(['A', 'B', 'C', 'D'], [1, 2, 3, 4, 5, 6]), null, 4);
<pre id="out"></pre>