Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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!


Comment on Re: Closest-value-in-list Golf!
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-11-28 04:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (193 votes), past polls