Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: (Golf) Nearest Neighbors

by danger (Priest)
on Apr 04, 2001 at 14:42 UTC ( #69611=note: print w/replies, xml ) Need Help??

in reply to (Golf) Nearest Neighbors

As Tortue points out, all the ones using lexigraphic sorting fail on some sets of numbers. Stripping leading spaces and newlines, my stab weighs in at 87 characters:

sub nn { @_=sort{$a<=>$b}@_; $_[1]-$_[0]>$_[$_+1]-$_[$_]and@_[0,1]=@_[$_,$_+1]for 1..@_-2; @_[0,1] }

Replies are listed 'Best First'.
Re: Re: (Golf) Nearest Neighbors
by MeowChow (Vicar) on Apr 04, 2001 at 23:45 UTC
    Very nice! Sniping one character gives:
    sub nn { @_=sort{$a<=>$b}@_; $_[1]-$_[0]>$_[$_]-$_[$_-1]and@_[0,1]=@_[$_,$_-1]for 2..$#_; @_[0,1] }
    The number to beat is 86...
                   s aamecha.s a..a\u$&owag.print
      This doesn't deal with duplicates. If you want to deal with duplicates, I think you change it to:
      sub nn { @_{@_}=0;@_=sort{$a<=>$b}keys%_; $_[1]-$_[0]>$_[$_]-$_[$_-1]and@_[0,1]=@_[$_,$_-1]for 2..$#_; @_[0,1] }

      That brings the total from 86 to 99. I'm not sure if the spec requires that duplicates be treated as one element or separate elements.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://69611]
[LanX]: yep heard the same origin story
[Your Mother]: I like it. I tottered on going into hucksterism because I feel like the world deserves it.
[LanX]: he ... we have a movement here called Anti-Germans based on this
[Corion]: Your Mother: I think that's because (in the west) the Nazi-Germans are recognized as universally evil. Of course, you could do some number games to calculate other measures of evil than "historic losers of second world war" to come up with other evils:)
[Corion]: I've heard "Troll" described as the new Punk, and in a way, it can be as destructive as living the Punk lifestyle, and you don't have to sit out in the cold...
[LanX]: Anti-Germans
[LanX]: Socrates was a Troll
[Your Mother]: It's very, very dangerous... Thinking that a group is intrinsically evil... buries the fact that all humans can be so deep that it starts to become likely they will be.
[Your Mother]: LanX++
[Corion]: (also the "troll for trolls sake" could seen be much like the "punk for punks beer")

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (17)
As of 2018-03-19 13:43 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (240 votes). Check out past polls.