Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^3: One Zero variants_without_repetition

by grinder (Bishop)
on Aug 07, 2007 at 13:49 UTC ( [id://631049]=note: print w/replies, xml ) Need Help??


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

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://631049]
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: (1)
As of 2024-04-16 15:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found