Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Generate all unique combinations of 1 and 0 using specified length of "0"-string and count of 1's

by salva (Canon)
on Sep 18, 2020 at 07:38 UTC ( #11121894=note: print w/replies, xml ) Need Help??


in reply to Re: Generate all unique combinations of 1 and 0 using specified length of "0"-string and count of 1's
in thread Generate all unique combinations of 1 and 0 using specified length of "0"-string and count of 1's

Generating all the combination and then filtering out those that don't have the right number of ones can be pretty inefficient, specially when the number of zeros is big and the number of ones small.

In any case, you can also use a regular expression for generating all the binary numbers of some given length :-)

my $length = 5; my $str = "0" x $length; my $start = $str . "1"; do { print "$str\n"; } while ($str =~ s/^(1*0)/substr $start, -length $1/e)
  • Comment on Re^2: Generate all unique combinations of 1 and 0 using specified length of "0"-string and count of 1's
  • Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11121894]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2021-04-20 17:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?