Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
How about using a string and shuffle the ones from right to left using substr.

use strict; use warnings; my $raCombinations = combinary(4, 7); print qq{$_\n} for @$raCombinations; sub combinary { my ($numZeros, $numOnes) = @_; my $str = q{0} x $numZeros . q{1} x $numOnes; my @combinations = ($str); my $leftPtr = 0; for my $thisOne ( 1 .. $numOnes ) { for ( my $offset = $numZeros + $thisOne - 2; $offset >= $leftPtr; $offset -- ) { substr $str, $offset, 2, q{10}; push @combinations, $str; } $leftPtr ++; } return \@combinations; }

produces

00001111111 00010111111 00100111111 01000111111 10000111111 10001011111 10010011111 10100011111 11000011111 11000101111 11001001111 11010001111 11100001111 11100010111 11100100111 11101000111 11110000111 11110001011 11110010011 11110100011 11111000011 11111000101 11111001001 11111010001 11111100001 11111100010 11111100100 11111101000 11111110000

I don't know whether this approach will be slower or faster than other suggestions. It's just the first idea that occurred to me.

Cheers,

JohnGG


In reply to Re: One Zero variants_without_repetition by johngg
in thread One Zero variants_without_repetition by thenetfreaker

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? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-02-03 23:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (30 votes). Check out past polls.

    Notices?