http://qs1969.pair.com?node_id=11121888

Lana has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks!

I am stuck with some trivial task. Maybe somebody could give me some idea on how to solve it.

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.

For example, I have a string of ten zeros - "0000000000", and I need to get all unique combinations using three ones. Like:

1110000000 1101000000 1100100000 1100010000 ...till... 0000100011 0000010011 0000001011 0000000111

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.

The count of "1" and length of "0" string may vary so I would prefer to have it as a subroutine, like:

sub GenUniStrings { my ($OnesCount, $ZeroStrLn) = @_; # some Perl magick goes here in loop, printing generated unique stri +ngs } GenUniStrings(3,25);

What is the best way to achieve that with minimal overhead and highest speed?

Thanks :)