Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: One Zero variants_without_repetition

by johngg (Canon)
on Aug 07, 2007 at 19:38 UTC ( #631126=note: print w/replies, xml ) Need Help??


in reply to One Zero variants_without_repetition

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

Replies are listed 'Best First'.
Re^2: One Zero variants_without_repetition
by graff (Chancellor) on Aug 08, 2007 at 04:07 UTC
    I gather that the OP also wants strings like:
    00011011111 00011101111 00011110111 00011111011 00011111101 00011111110 00110011111 00111001111 ...
    which your code didn't get around to. (And apparently, expecting to hold all results in memory before outputting them may be unrealistic.)
Re^2: One Zero variants_without_repetition
by thenetfreaker (Friar) on Aug 08, 2007 at 06:13 UTC
    You are right about these strings that it doesn't get, as well as:
    . . . 01001011111 . . . 11011001011 . . etc

    but i don't need to hold them in memory, i simply check each one of them individually and move to the other;
    but this algorithm surely seems to work Fast.
    If only to fix it a bit to show every variation like the ones you stated, considering it shall slow in twice or three times, with will be wonderful.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2022-12-03 18:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?