Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: One Zero variants_without_repetition

by GrandFather (Saint)
on Aug 07, 2007 at 11:03 UTC ( [id://631010]=note: print w/replies, xml ) Need Help??


in reply to One Zero variants_without_repetition

Presuming you don't care about the order, the following may be what you are after:

use strict; use warnings; for my $left (1 .. 4) { for my $right (0 .. $left -1) { printf "%05b\n", (1 << $left) | (1 << $right); } }

Prints:

00011 00101 00110 01001 01010 01100 10001 10010 10100 11000

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: One Zero variants_without_repetition
by thenetfreaker (Friar) on Aug 07, 2007 at 11:11 UTC
    i changed a bit you code to :
    my $places = 6; for my $left (1..$places-1) { for my $right (0 .. $left -1) { printf "%0$places"."b\n", (1 << $left) | (1 << $right); } }
    and saw it works only with 2 ones...

    and what if i have 6 ones and 14 zeroes ? do i have to do 6 for() loops ???

      Recursion:

      use strict; use warnings; doShift (2, 5); print "\n"; doShift (3, 6); print "\n"; sub doShift { my ($ones, $bits, $pattern, $limit) = @_; --$ones; $limit ||= $bits; $pattern ||= 0; for my $right ($ones .. $limit - 1) { if ($ones) { doShift ($ones, $bits, $pattern | (1 << $right), $right); } else { printf "%0*b\n", $bits, $pattern | (1 << $right); } } }

      Prints:

      00011 00101 00110 01001 01010 01100 10001 10010 10100 11000 000111 001011 001101 001110 010011 010101 010110 011001 011010 011100 100011 100101 100110 101001 101010 101100 110001 110010 110100 111000

      DWIM is Perl's answer to Gödel
        GrandFather spake it well; this question maps quite well into a recursive algorithm:
        sub combinatoric($$); # prototype forward reference sub combinatoric( $$ ) { my ( $zero, $one ) = @_; my @ret = (); if ( $one == 0 ) { # no more ones left to select, the rest is zeroes. push @ret, scalar ( '0' x $zero ); } elsif ( $zero == 0 ) { # no more zeroes left to select, the rest is ones. push @ret, scalar ( '1' x $one ); } else { # take a zero from the pile, and compute what's left push @ret, "0$_" foreach combinatoric( $zero-1, $one ); # take a one from the pile, and compute what's left push @ret, "1$_" foreach combinatoric( $zero, $one-1 ); } return @ret; } print "$_\n" foreach combinatoric( 10, 10 );
        I was quite surprised to see how quickly the 10, 10 case started giving results, even on a lowly 2.4GHz Xeon...
        That's nice, but it goes through the factorial of $ones+$zeroes, 5!=120.. 120 != 10.
        i'm working with strings of 435 zeroes and 343 ones (for instance), where i'm in dought i'll live that long to see it finish.

        i need this sub{} to play with the strings that contain that number of 1's and 0's, not the numbers that contain them.

Log In?
Username:
Password:

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

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

    No recent polls found