Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re (tilly) 1: (Golf) Nearest Neighbors

by tilly (Archbishop)
on Apr 05, 2001 at 00:29 UTC ( [id://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 }
(75...)

UPDATE
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...

UPDATE 2
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...

UPDATE 3</B
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.

    p
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...
       MeowChow                                   
                   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; } @_; @_ }
    John.
    --


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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-03-28 16:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found