views:

52

answers:

2

Suppose there is array

main[] = [a,b,c,....z];
a[] = [a1,a2,a3,....] ,
b[] = [b1,b2,b3,....]

and so on.

So if we generate combination in main.length of ordered then it looks like .....
a1 b1 c1, a1 b1 c2, a1 b1 c3 ......
a1 b2 c1, a1 b3 c1, .....
a2 b1 c1, a3 b1 c1, .....

and total count will be a.length * b.length * c.length ........ = ?

So write a code in any language to generate those combination. all the best.

alex

A: 
var combine = function(a) {
  var fn = function(n, src, got, all) {
    if (n == 0) {
      if (got.length > 0) {
        all[all.length] = got;
      }
      return;
    }
    for (var j = 0; j < src.length; j++) {
      fn(n - 1, src.slice(j + 1), got.concat([src[j]]), all);
    }
    return;
  }
  var all = [];
  for (var i=0; i < a.length; i++) {
    fn(i, a, [], all);
  }
  all.push(a);
  return all;
}

above code generates combine([1,2]) == [[1], [2], [1,2]];
but this does not meet my requirement is describe above.

roconmachine
Please post the above code as an update to the existing question.
Levi Hackwith
A: 
function combination(x,selRow,currCol)
{
    if(currCol==x.length-1)
    {
        for(var i = 0; i < x[currCol].length; i++)
        {
            g_com.push(g_temItem.join("") + x[currCol][i]);
        }
        g_temItem.splice(g_temItem.length -1,1)

        return;
    }
    else
    {
        for(var i = 0; i < x[currCol].length; i++)
        {
            g_temItem.push(x[currCol][i]);
            combination(x,selRow,currCol + 1);
        }
        g_temItem.splice(g_temItem.length -1,1)
    }


}

here is my required code. recursion help me to solve the problem.

I have a comments for you people. you can comments what ever you want but those questions are technical not literary.

roconmachine