Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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

Replies are listed 'Best First'.
(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 surveying the Monastery: (12)
As of 2019-05-27 12:25 GMT
Find Nodes?
    Voting Booth?
    Do you enjoy 3D movies?

    Results (156 votes). Check out past polls.