Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

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.

Replies are listed 'Best First'.
Re: Re (tilly) 1: (Golf) Nearest Neighbors
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.

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
Re: Re (tilly) 1: (Golf) Nearest Neighbors
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; } @_; @_ }

Re: Re (tilly) 1: (Golf) Nearest Neighbors
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 ;)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://69875]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2017-10-19 04:23 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (251 votes). Check out past polls.