Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Closest-value-in-list Golf!

by chipmunk (Parson)
on May 29, 2001 at 20:26 UTC ( #83991=note: print w/replies, xml ) Need Help??


in reply to Closest-value-in-list Golf!

I disagree with tilly that using sort() is the natural solution. I think this is the natural solution:
sub closest { $n=pop;$c=pop;abs$n-$_<abs$n-$c?$c=$_:0for@_;$c }
This is O(n) rather than O(n*log n). Of course, at 47 characters it is, unfortunately, a longer solution. Oh well!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://83991]
help
Chatterbox?
[marto]: Re: Monastery login over http
[Corion]: Veltro: This is due to the fact that one of our webservers doesn't have "our" https certificate but another https certificate. Work is underway (well, on my long, long to-do list) to switch even that server over to "our" certificate

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (10)
As of 2018-05-23 11:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?