Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Code Shortening (golf)

by eyepopslikeamosquito (Archbishop)
on Oct 18, 2010 at 20:55 UTC ( [id://866046]=note: print w/replies, xml ) Need Help??


in reply to Code Shortening (golf) [RESOLVED]

My general golfing advice is to try to use $\ as the accumulator if you possibly can because you can then print it with a bald print.

For example, this one is six strokes shorter and gives the same result as yours for the three test cases given at the spoj web site:

$\+=~y///c&3for<>=~/([a-z]+)/g;print
Don't know if it's generally valid though.

Replies are listed 'Best First'.
Re^2: Code Shortening (golf)
by Young Monk (Novice) on Oct 19, 2010 at 17:30 UTC
    It will fail for a test case like AB for which, it must be A---B, (ie) output is 3.
    In my solution, decrementing 3 from the result is necessary. So, a bald 'print' cant be used! Any suggestions?

      So, a bald 'print' cant be used!
      To be a competitive golfer you have to be tenacious, devious and challenge assumptions. So I wouldn't rule out using a bald print just yet. At least, not without first trying to find a way to adjust your algorithm so that it can be used.

      For example, why do you subtract 3 at the end, as in print$n-3? Is that to compensate for adding 3 when you (always) match at start of string? If so, you could try to eliminate the need to subtract 3 by adjusting your regex to not match at start of string.

      In any case, I suggest you study every line of perlre looking for something, anything, that might help you shave a stroke. That is the attitude required to win at golf.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-24 04:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found