in reply to (Golf) Nearest Neighbors
sub nn{my$x=pop;(sort{$b<=>$a}map{abs($_-$x)}@_)[0,1]}
I guess that is 46 chars.
Update: Sorry (oops), make that:
for 69 or 68 chars.sub nn{my$x=pop; (map$_->[0],sort{$a->[1]<=>$b->[1]}map[$_,abs$_-$x],@_)[0,1]} # or sub nn2{my$x=pop; my@d=map abs$_-$x,@_;@_[(sort{$d[$a]<=>$d[$b]}0..$#_)[0,1]]}
Update: Well, those last two are okay solutions for the wrong problem. *sigh* (:
- tye (but my friends call me "Tye")
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (tye)Re: (Golf) Nearest Neighbors
by tilly (Archbishop) on Apr 04, 2001 at 22:45 UTC | |
by tye (Sage) on Apr 04, 2001 at 23:20 UTC | |
by tilly (Archbishop) on Apr 05, 2001 at 00:00 UTC | |
by MeowChow (Vicar) on Apr 04, 2001 at 23:26 UTC | |
by tye (Sage) on Apr 04, 2001 at 23:39 UTC | |
by MeowChow (Vicar) on Apr 04, 2001 at 23:41 UTC |