Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Your skill will accomplish
what the force of many cannot

Re (tilly) 1: (Golf) Nearest Neighbors

by tilly (Archbishop)
on Apr 05, 2001 at 00:29 UTC ( #69875=note: print w/ replies, xml ) Need Help??

in reply to (Golf) Nearest Neighbors

Why have a loop?

sub nn { my@x=@_[0,1];@_=sort{abs$x[0]-$x[1]<abs$a-$b or@x=($a,$b);$a<=>$b}@_;@ +x }

MeowChow pointed out that the parens for the arguments of abs were not needed. That cut out 3 characters. Plus by reversing the order of the comparison I managed to substitute or for and, saving another. That makes it 71...

This is sick and slick.

sub nn { ()=sort{abs$_[0]-$_[1]<abs$a-$b or@_=($a,$b);$a<=>$b}@_;@_ }
Enjoy verifying that I am perfectly safe in using @_ as my temporary array. I think that 58 is the best I can do...

petral sent me a /msg explaining how to remove 3 chars from that solution. I would like to see him post that since it was his idea, but until he does I want to note that that solution can be beaten.

Comment on Re (tilly) 1: (Golf) Nearest Neighbors
Select or Download Code
Re: Re (tilly) 1: (Golf) Nearest Neighbors
by MeowChow (Vicar) on Apr 05, 2001 at 01:03 UTC
    Wow, that really is quite ingenious. At first I thought this might fail on some inputs, if sort doesn't compare two sequential numbers, but then I realized that sort requires that every two sequential elements are compared (in some order). Amazing...
                   s aamecha.s a..a\u$&owag.print
by jmcnamara (Monsignor) on Apr 05, 2001 at 16:36 UTC

    Excellent solution. The interesting thing about Perl Golf is how different people tackle problems in terms of their favourite tools; map, sort, regex, grep, closures, slices or whatever.

    Just for fun I ran your solutions through Perltidy as a test case. Here is the output:
    sub nn { my @x = @_[0, 1]; @_ = sort { abs $x[0] - $x[1] < abs $a - $b or @x = ($a, $b); $a <=> $b; } @_; @x } sub nn { () = sort { abs $_[0] - $_[1] < abs $a - $b or @_ = ($a, $b); $a <=> $b; } @_; @_ }

by bobione (Pilgrim) on Apr 05, 2001 at 17:38 UTC
    Okay Monks,
    I would like to make my contribution of this golf, but after tilly's one, I can't continue. Maybe I'll find a 120 characters so :) ... that over for me.

    BoBiOne KenoBi ;)
by petral (Curate) on Apr 06, 2001 at 08:34 UTC
    OK, a couple of simple typographic manipulations to bring it to 55:
    sub nn { ()=sort{abs$a-$b>abs$_[0]-$_[1]or@_=($a,$b);$a-$b}@_;@_ }
    (There are advantages to being slow (or having no time!)).  I was still up over 80 strokes with my sort-based solution when I looked up and saw this guy casually strolling past Tiger Woods.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-03-17 20:50 GMT
Find Nodes?
    Voting Booth?

    Have you used a cryptocurrency?

    Results (358 votes), past polls