XP is just a number | |
PerlMonks |
Re: Generate all unique combinations of 1 and 0 using specified length of "0"-string and count of 1'sby johngg (Canon) |
on Sep 18, 2020 at 10:07 UTC ( [id://11121910]=note: print w/replies, xml ) | Need Help?? |
This problem was visited before here and after a stupid first attempt I posted this solution, which I show again here. Note that my "permutary" routine should more accurately be called "combinatory" or something like! Posted here is code that will work for strings up to 50 or so ones and zeros but there is a version using Math::BigInt for longer strings included here.
I'm not sure how it would stack up in the benchmarks but I seem to recall that a regex solution from that 2007 thread was faster. Update: Regarding GrandFather's note about the limitation of 32-bit Perl in his benchmark, I developed the code on 64-bit Perl and hit a limit at about 53 digits for either zeros or ones. The Math::BigInt version's only limit was the user's patience! Cheers, JohnGG
In Section
Seekers of Perl Wisdom
|
|