I'm working on a way to solve this problem : Given an amount of 0 and 1, generate the list containing all the possible combinations of 0 and 1.
Example : if we have one 1 and two 0, the algorithm will return
001
010
100
The problem is that the classic combination algorithms are not optimized for this purpose. This is the result a non optimized combination algorithm would return :
001
001
010
010
100
100
As you can see, all combinations are repeated twice because 0 are interpreted as different elements.
How an algorithm can be made to generate the list of possible combination inputting the number of 0 and 1, without repeating combination ?
PS : This is will be used in Javascript
EDIT : Solved ! Using @Batch method.
function combin(o, i)
{
if (o == 0 && i > 0)
{
for (var j = 0, s = ''; j < i; j++)
{
s += '1';
}
return [s];
}
else if (i == 0 && o > 0)
{
for (var j = 0, s = ''; j < o; j++)
{
s += '0';
}
return [s];
}
else if (i == 0 && 0 == o)
{
return [''];
}
else if (i > 0 && o > 0)
{
var l = combin(o - 1, i);
for (var j in l)
{
l[j] = '0' + l[j];
}
var k = combin(o, i-1);
for (var j in k)
{
k[j] = '1' + k[j];
}
return l.concat(k);
}
}