Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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 :)


In reply to Generate all unique combinations of 1 and 0 using specified length of "0"-string and count of 1's by Lana

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 06:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found