in reply to Golf: overtone calculator

Yours unseen, this is scarily close, but I pipped you by 1!

#! perl -w sub H($$){ map int(1e3*$_[0]*$_)/1e3,grep!$^H{$_}++,sort map{$.=$_;map$./$_,1..$_ +}1..$_[1] } $"=$/;print "@{[H(100,6)]}\n"; __END__ C:\test>194468 100 120 125 133.333 150 166.666 200 250 300 400 500 600 C:\test>

UpdateSlight improvement 77. Nowhere near jynx's, but ...both mine comply with the spec!

Neither yours nor Jynx's do! You showed trailing zero supression 8^). (That's my nose in the air:)

sub H($$){ map{int(1e3*$_*$_[0])/1e3}grep!$_{$_}++,sort map{$.=$_;map$./$_,1..$_} +1..pop }

Well It's better than the Abottoire, but Yorkshire!

Replies are listed 'Best First'.
Re: Re: Golf: overtone calculator
by Django (Pilgrim) on Sep 01, 2002 at 23:24 UTC

    Trailing zero supression wasn't intended (fixed it).
    Would you mind explaining your code? I'm just examining it for the nth time and still can't get no grip on it...

    ~Django
    "Why don't we ever challenge the spherical earth theory?"

      Is it so different from yours?

      Nested map's -v- nested for's

      Instead of using sprintf, I multiply the result by 1e3 int it and divide by 1e3 again.

      Which bit gave you problem's?

      BTW. I noted your comment re: jynx's and lexical sorting when n>10, and realise that mine suffers the same flaw. So, numerisising(is that a word?) the sort, I got

      #! perl -w sub H($$){ #________1_________2_________3_________4_________5_________6_________7 +_________8____ #234567890123456789012345678901234567890123456789012345678901234567890 +12345678901234 map{int(1e3*$_*$_[0])/1e3}grep!$_{$_}++,sort{$a<=>$b}map{$.=$_;map$./$ +_,1..$_}1..pop }

      The extra 9 chars* cost me dear, and you were winning by 3, but I couldn't have that so...for a round of 76:

      sub H($$){ #________1_________2_________3_________4_________5_________6_________7 +______ #234567890123456789012345678901234567890123456789012345678901234567890 +123456 map{$.=$_,map$_{int($_[0]*1e3*$./$_)/1e3}++,1..$_}1..pop;sort{$a<=>$b} +keys%_ }

      but that meant using a map in a void context so for the same 76, but avoiding that pesky map

      sub H($$){ #________1_________2_________3_________4_________5_________6_________7 +______ #234567890123456789012345678901234567890123456789012345678901234567890 +123456 $.=$_,map$_{int($_[0]*1e3*$./$_)/1e3}++,1..$_ for 1..pop;sort{$a<=>$b} +keys%_ }

      By now the light was fading and it was hard to keep my eye on the ball, so bad light stopped play and the tournement can resume tomorrow with jynx first to step up to the tee:)

      Update However, during floodlite play, (and stealing jynx's best bits), and retaining the more aesethically pleasing trailing zero suppression:), I give you this for 71

      sub H($$){ #________1_________2_________3_________4_________5_________6_________7 +_ #234567890123456789012345678901234567890123456789012345678901234567890 +1 for$.(1..pop){$_{int($_[0]*1e3*$./$_)/1e3}++for 1..$.}sort{$a-$b}keys% +_ }

      * I officially motion, on behalf of golfer's everywhere, that Perl6 have a sortn built-in that assumes the $a<=>$b. (Along with rev as an alias for reverse 8^).


      Well It's better than the Abottoire, but Yorkshire!

        Shamelessly stealing from jynx and you, I contribute my meager -2 strokes for 69:

        #________1_________2_________3_________4_________5_________6_________ #23456789012345678901234567890123456789012345678901234567890123456789 for$a(1..pop){$_{1e-3*int"@_"*1e3*$a/$_}++for 1..$a}sort{$a-$b}keys%_

        — Arien

        OK, thanks for the notes - I think I got it now.
        What confused me with your first attempt were quite a few things:
        nested maps, that 1e3 trick, the freakish use of $. and especially that sick thing:
        grep !$^H { $_ }++
        Your current version is much straighter (not saying less tricky), and admittedly trailing zero suppression IS aesthetically more pleasing (shame on me!).

        ~Django
        "Why don't we ever challenge the spherical earth theory?"