Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^2: One Zero variants_without_repetition

by thenetfreaker (Friar)
on Aug 07, 2007 at 12:57 UTC ( [id://631034]=note: print w/replies, xml ) Need Help??


in reply to Re: One Zero variants_without_repetition
in thread One Zero variants_without_repetition

Extrordinaly nice, but whay does it do extra checks - crop some results ?
i simply added a counter($inC=0), in the beginning and told it to ++ before --$cand;, and at the end it printed 14 instead of 10, and only ehen i put the $inC++ before the print in the while() it printed 10.
i'm sorry to repeat that i work with hundreds of 1's and 0's, and every false checking costs.

All of your code are wonderful, but unfortunatly i like the second code i posted the most, it only needs to get fixed in the part where it gets the $Oc and $Zc; i would have replaces this while() to a foreach() of an array that contains all the distances between ones and zeroes sets(e.g, in the string 0010111 the distance's array should look like qw(2 1 1 3)), but for that i need to know all the distances variaties( without repetition) from @distances= ($ones, $zeroes) upto @distances= ($zeroes, $ones) when the @distances reach the array of $ones+$zeroes-1 times 1 ( if $ones=2 and $zeroes=3, when @distances=(1,1,1,1,1)) the rest of the @distances are the reverse of the previose sets in reverse:
11000 [2 3] 10100 [1 1 1 2] 10010 [1 2 1 1] 10001 [1 3 1] 10011 [1 2 2] 01010 [0 1 1 1 1] 01001 [0 1 2 1] 00110 [0 2 2 1] 00101 [0 2 1 1 1] 00011 [0 3 2]

Replies are listed 'Best First'.
Re^3: One Zero variants_without_repetition
by grinder (Bishop) on Aug 07, 2007 at 13:49 UTC
    Extrordinaly nice, but whay does it do extra checks - crop some results ?

    What extra checks? It just hunts through the array, looking for a 0, 1 pair to swap to 1, 0. If it does so, it sorts the tail of the array that it has already walked past so that 0, 1, 1, 0 becomes 1, 0, 0, 1 (instead of 1, 0, 1, 0). The if check is just to avoid sorting empty length sub-arrays.

    All of your code are wonderful, but unfortunatly i like the second code i posted the most

    Yeah, but if it produces garbage, what's the point? There's no point in hanging onto code that doesn't work. Your problem interests me as an intellectual challenge, but I cannot summon the motivation to debug your code :)

    I imagine my code would be very efficient up to several hundred elements. At some point it would become more efficient to examine the tail, count the 0s and 1s, and splice in a newly-constructed tail on the fly, thereby avoiding the sort:

    my ($zero, $one) = (0, 0); for my $element (@array[$cand+1..$#array]) { $element || ++$zero; $element && ++$one; } @array[$cand+1..$#array] = ((0) x $zero, (1) x $one);

    The counting of 0s and 1s is a tad ugly, I admit, but it avoids creating a lexical scope that a classic if/else block would involve.

    • another intruder with the mooring in the heart of the Perl

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-16 12:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found