develooper Front page | perl.golf | Postings from July 2002

Re: My solution annotated

Thread Previous | Thread Next
From:
Jasper McCrea
Date:
July 8, 2002 08:30
Subject:
Re: My solution annotated
Message ID:
3D29AFF9.FB94623A@guideguide.com
Ala Qumsieh wrote:
> 

snip

> 
> ## This can be re-written as:
> ##     while (/\S+/g) {
> ##         if (!/ \Q$&\E\n/) {
> ##             print $&;
> ##             s/^\Q$&\E\s//mg;
> ##         }
> ##     }
> ##

I didn't realise that a while loop like this is restarted if $_ changes.
I attempted a solution along these lines not knowing this, and it came
out way longer than my final sorting method because I had to mess about
so much. bugger, bugger, bugger...

My final solution was pretty simple: (had to scroll _way_ down to find
it..)

-lp0 @e{@a=split}=1;$o=join$\,keys%e;1/!eval'grep/
/&$o=~s#^(\Q$\'\E)$((.|
)*
)(\Q$`\E)$#$4$2$1#m,/.*/g;'x@a;$_=$o

Create a unique string of return separated elements, and get a decent
sized array number to loop over for. Then go through the nodes one by
one, swapping elements that are in the wrong order. If, on the last run
through, any swaps take place, the eval returns true, and the thing dies
with that old divide by 0 error.

I think the referees took pity with me on the time front. For the bigger
tests, the use of $' and $` really killed it, and it took several minutes
on my very slow 300.

Still, I beat Andrew Savige and Yanick, which I certainly did not do on
tpr04.

Jasp
-- 
Homer: Caught by the very man who was trying to catch him...
       How ironic.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About