Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2017-08-22 11:39 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (333 votes). Check out past polls.