develooper Front page | | Postings from July 2002

Re: My solution annotated

Thread Previous | Thread Next
Jasper McCrea
July 8, 2002 08:30
Re: My solution annotated
Message ID:
Ala Qumsieh wrote:


> ## 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

-lp0 @e{@a=split}=1;$o=join$\,keys%e;1/!eval'grep/

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

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

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About