0

The following code here - Generating combinations from n arrays with m elements - is almost what i am looking for, however, what if i needed to take a different number of elements from each array.

consider this data...

[[0,1,2,3,4], 
 [0,1,2,3,4,5,6], 
 [0,1,2,3,4,5,6,7],
 [0,1,2,3],
 [0,1,2,3,4],
 [0,1,2,3,4]
]

What if i wanted to take 1 element from the first array, 2 from the second, 3 from the third, and then 1 from the rest, for a total of 9 elements per output array.

        function cartesian() {
            var r = [], arg = arguments, max = arg.length-1;
            function helper(arr, i) {
                for (var j=0, l=arg[i].length; j<l; j++) {
                    var a = arr.slice(0); // clone arr
                    a.push(arg[i][j])
                    if (i==max) {
                        r.push(a);
                    } else
                        helper(a, i+1);
                }
            }
            helper([], 0);
            return r;
        };

The code above works nearly perfectly, but it only grabs one element from each array, as obviously the arg.length is only seeing six arguments and not the preferred 1 from arg1, 2 from arg2, 3 from arg3, 1 from arg4, 1 from arg5 and 1 from arg6.

Finally, but not as important (I'd like to try to figure this part out on my own if I can), from one of the groups that I'm grabbing multiple elements, each element can only be used once.

I should mention, that each element is actually a json object, not just a simple number. With the above code, it doesn't matter though.

If it helps, I can calculate the total NUMBER of possiblities with the following:

//this is not in any particular language, just a large calculation
//its a simple factorial for Combinations - n!/r!(n-r)!
//lets say we have only 10 objects in each of the 9 arrays...
//10 * 9 = total of 90 objects in the pool.
(factorial(90))/((factorial(9))*(factorial(90 - 9))) = **706,252,528,634**

The number gets really high as each array can contain up to 80 objects.

Thanks.

UPDATE: After playing around quite a bit, I've gotten very far...now i have a completely different problem which i will likely re-post somewhere else. The output here is MASSSIIIVVVEEEE. In the quadrillions, and that's if i only take a small sample set like 10 per array. Javascript just runs out of memory and crashes in all tested major browsers (chrome, ie, firefox, safari). Will post as another question and then post link here.

Community
  • 1
  • 1
  • 1
    Replace the second array with an array containing all pairs of elements, the third array with an array containing all triplets, and all the others with arrays containing 1-element sub-arrays. Then use your combination loop, combining all these sub-arrays. – Barmar Jan 01 '15 at 16:55
  • clever. just have to figure out how to bubble them up in the final arrays because they will of course be nested. But A+ for simple logical thinking, working on it now. – Matthew Martini Jan 01 '15 at 21:08

1 Answers1

0
count = 0;
past = false;
arr = [];
data = [[0,1,2,3,4], [0,1,2,3,4,5,6], [0,1,2,3,4,5,6,7], [0,1,2,3], [0,1,2,3,4], [0,1,2,3,4]];

for(i=0; i<data.length; i++){ //iterate through data length
    if(count<3 && !past){ //check if it should go up 1
        count++;
    }else if(!past){ //reset to 1
        count = 1;
        past = true;
    }
    arr.push(data[i][count]); //add to new array
}

alert(arr); //alert final result
Isaiah Taylor
  • 615
  • 1
  • 4
  • 17