Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Closest-value-in-list Golf!

by suaveant (Parson)
on May 29, 2001 at 17:57 UTC ( #83927=note: print w/ replies, xml ) Need Help??


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

Why are you using strict in golf if it is not required?

sub g {$n=pop;$h{abs($_-$n)}=$_ for@_;@s=sort{$a<=>$b}keys%h;$h{shift@ +s};}
66 chars with strict off

Update

sub g {$n=pop;$h{abs($_-$n)}=$_ for@_;@s=sort{$a<=>$b}keys%h;$h{shift@ +s}}
oops trailing ; is extraneous... 65 strokes...

Update2 oooh!

sub g {$n=pop;$h{abs($_-$n)}=$_ for@_;$h{(sort{$a<=>$b}keys%h)[0]}}
59 strokes
                - Ant


Comment on Re: Closest-value-in-list Golf!
Select or Download Code
(boo)Re: Re: Closest-value-in-list Golf -- or, a sorted affair.
by boo_radley (Parson) on May 29, 2001 at 18:38 UTC
    sort{$a<=>$b}keys%h
    is too much, since $a<=>$b is the default sort. that leaves you with
    sub g {$n=pop;$h{abs($_-$n)}=$_ for@_;$h{(sort keys%h)[0]}}
    for 52.
      did you test this? i think you'll find that $a cmp $b is the default.

      "Argument is futile - you will be ignorralated!"

      Test your code...

      that does not work... I tried it already... returns 20
                      - Ant

        How strange. I was using a different data set when I tested this, and it returned the correct answer.

        Mea culpa.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2015-07-04 08:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (58 votes), past polls