Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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


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
    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?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (13)
As of 2014-09-17 15:08 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (87 votes), past polls