more useful options PerlMonks

### (tye)Re: (Golf) Nearest Neighbors

by tye (Sage)
 on Apr 04, 2001 at 22:32 UTC ( #69781=note: print w/replies, xml ) Need Help??

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:

```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]]}
for 69 or 68 chars.

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
```print nn(1, 4, 7);
returns 2 elements that are not in the input list.

UPDATE

```print nn(1, 5, 7);
print nn2(1, 5, 7);
# Hmmmm
print nn(1, 5, 11);
print nn2(1, 5, 11);
after your first update all result in 51. Care to try again? :-)

Just following the (ambiguous) specification. Are you looking for numbers from (5,11) that are close to 1 or numbers from (1,5) that are close to 11? I didn't find an API spec and found others using pop so I went ahead with the 2-character savings.

Or am I only supposed to return one number if the "two closest" are both on "the same side" of the search-for number? That wasn't clear to me either so I just went with "return the two closest" without trying to assume a bunch of extra subtle meaning to that phrase. No, I'm not going to produce a version that sometimes returns only one number. (:

- tye (but my friends call me "Tye")
I think the "search-for" number is a misunderstanding on your part. We are not looking for the number that is closest to the first. We are looking for the pair of numbers, out of all n choose 2 pairs, which are closest together.

So given (1, 5, 7) it would return (5, 7) (or (7, 5)) while (1, 5, 11) would return (1, 5). And you cannot assume that it is presented in increasing order.

Any solution which returns a pair of numbers that are in the list and represent a minimal distance from each other with respect to all other possible pairings is correct. We are comparing the distance between all possible number pairings, and returning a minimal one. So the following are all correct:
```nn(1,5,9,15) returns (1,5)
# or
nn(1,5,9,15) returns (5,9)
# or
nn(1,5,9,15) returns (9,5)
I don't really understand the beginning of your question though.
```   MeowChow
s aamecha.s a..a\u\$&owag.print```

Create A New User
Node Status?
node history
Node Type: note [id://69781]
help
Chatterbox?
 [erix]: how does one find out how to install a program like cvsgrep [erix]: I mean, I suppose it's just the one file so I can download that [erix]: Makefile.PL should tell me, probably

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2017-06-22 16:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (524 votes). Check out past polls.