Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hello vagabonding electron,

Good job! In playing around with the code, I found that the @tmp array can be removed, along with half of the “ugly hack” —

#! perl use strict; use warnings FATAL => 'all'; use experimental 'smartmatch'; use List::MoreUtils 'zip'; use Test::More tests => 7; while (<DATA>) { my ($left, $right) = split /->/; my @array = split ' ', $left; my @expected = split ' ', $right; my $aa = unsort(\@array); ok( @$aa ~~ @expected ); } sub unsort { my $aref = shift; @$aref = sort { $a <=> $b } @$aref; my %nums = map { $_ => undef } @$aref; return $aref if scalar keys %nums == 1; my $half = int(($#$aref + 2) / 2); if (scalar keys %nums == 2) { if ($aref->[$half - 1] == $aref->[$#$aref]) { @$aref = (@$aref[$half .. $#$aref ], @$aref[0 .. $half - 1]); @$aref[$half - 1, $#$aref] = @$aref[$#$aref, $half - 1]; } } else { for my $i (0 .. $half - 1) { last if $i + $half > $#$aref; if ($aref->[$i] == $aref->[$i + $half - 1]) { @$aref = (@$aref[$i .. $#$aref], @$aref[0 .. $i - 1]); } } } my @first = @$aref[0 .. $half - 1]; my @second = @$aref[$half .. $#$aref ]; @$aref = zip @first, @second; pop @$aref unless defined $aref->[-1]; return $aref; } __DATA__ 0 0 1 -> 0 1 0 0 1 1 -> 1 0 1 1 2 3 3 2 2 1 -> 1 2 1 3 2 3 2 3 2 1 2 1 3 2 -> 1 2 1 3 2 3 2 1 3 3 3 3 1 1 -> 3 1 3 1 3 1 3 1 1 1 2 3 -> 1 2 1 3 1 1 1 1 2 3 3 2 2 1 -> 1 2 1 2 1 3 1 3 2

Hope this is of interest,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Most shuffled array by Athanasius
in thread Most shuffled array by vagabonding electron

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?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-04-19 06:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found