perlquestion
Lana
<p>Hello Monks!</p>
<p>I am stuck with some trivial task. Maybe somebody could give me some idea on how to solve it.</p>
<p>I need to generate all possible combinations of "0" and "1" - at the specified lenght of "0"'s string and using specified count of "1"'s.</p>
<p>For example, I have a string of ten zeros - "0000000000", and I need to get all unique combinations using three ones. Like:</p>
<code>1110000000
1101000000
1100100000
1100010000
...till...
0000100011
0000010011
0000001011
0000000111</code>
<p>It seems to be easy to do on a 10-zeros string just by iterating 1024 binary numbers and filtering out those not having three ones and seven zeros. But when it comes to long string of zeros, say 40, I have to iterate an enormous one trillion binary numbers which semms to be a very bad solution with giant overhead.</p>
<p>The count of "1" and length of "0" string may vary so I would prefer to have it as a subroutine, like:</p>
<code>sub GenUniStrings {
my ($OnesCount, $ZeroStrLn) = @_;
# some Perl magick goes here in loop, printing generated unique strings
}
GenUniStrings(3,25);</code>
<p>What is the best way to achieve that with minimal overhead and highest speed?</p>
<p>Thanks :)</p>