Generate all unique combinations of 1 and 0 using specified length of "0"-string and count of 1'sby Lana (Beadle)
|on Sep 18, 2020 at 03:17 UTC||Need Help??|
Lana has asked for the wisdom of the Perl Monks concerning the following question:
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:
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:
What is the best way to achieve that with minimal overhead and highest speed?