Beefy Boxes and Bandwidth Generously Provided by pair Networks Ovid
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

In golf, there are many more failed attempts than successful breakthroughs. That's the nature of the game. To keep the articles reasonably short, I focused on the breakthroughs, omitting many of the failures. To add a dose of reality, and while I'm still able to remember, I thought I'd describe some of my more interesting "heroic failures" in this game. You never know, someone may find an improvement and transform one of these failures into a winner.

Mapping to Different Number Bases

Mapping to different number bases is a generally useful golfing technique. While I couldn't find a useful number base mapping for the original M -> 1000, D -> 500, C -> 100, L -> 50, X -> 10, V -> 5, I -> 1, I spied an opportunity later in the game after uncovering the alternative M -> 2000, D -> 1000, C -> 200, L -> 100, X -> 20, V -> 10, I -> 2 mapping, illustrated in the following table.

Romanoctaldecimal1<<n2<<n
M20001024109
D100051298
C20012876
L1006465
X201643
V10832
I2210

Noticing this cute mapping led directly to the following Python 82 stroker:

t=p=0 for r in raw_input():n=int(oct(2<<6155848/ord(r)%11));t+=n/2-p%n;p=n print t
As you can see, this solution was not competitive in this game only because of the five stroke Python string to int conversion penalty of having to call int() -- which wouldn't have been required in Perl or PHP. This idea was similarly foiled in Ruby by the need for string to int conversion.

Equally cruelly, in a language where the string to int conversion is not required, namely PHP, this idea was crushed by the arbitrariness of PHP's internal function names. You see, PHP calls this function not oct, like any reasonable language, but decoct, costing three precious strokes. Aaargh. Finally, in Perl, the oct function converts the other way and I couldn't find any very short way to convert from decimal to octal.

Eliminating Exponentiation

Though exponentiation is "natural" for Roman numerals, it does cost around six strokes (10**()), leaving the door open for shorter, if less natural, alternatives -- such as the PHP md5 lucky hit found in the third article of this series.

Though less ideal than PHP's md5 function, Python's built-in hash function seems the best hope of eliminating exponentiation among the other three languages. Indeed, this 79 stroker proved only one stroke too long:

t=p=0 for r in raw_input():n=hash(r+"VFkQW")%291*5%1254;t+=n-2*p%n;p=n print t
This formula should be able to be further shortened with a more direct mapping of a longer magic string, such as:
n=hash(r+"magicstring")%1234
Though such a solution should exist -- just like the "proven" shorter PHP md5 solutions -- I was unable to write a fast enough search program to find one.


In reply to Re: The golf course looks great, my swing feels good, I like my chances (Part II) by eyepopslikeamosquito
in thread The golf course looks great, my swing feels good, I like my chances (Part II) by eyepopslikeamosquito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (14)
    As of 2013-05-18 18:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best material for plates (tableware) is:









      Results (394 votes), past polls