How to learn golfing in Python (or: How to become a golfing god like Mark Byers in seven steps :)

Add a reply

Posted by hallvabo 557 days ago:
Disclaimer: This tutorial reflects how I learned how to golf and rose to be among the top 10 Python golfers. Others might have taken entirely different paths.


STEP 1:

First, get the official documentation for Python 2.5.

The most interesting sections are

* 2.1 Built-in Functions
* 3.6 Sequence Types
* 3.6.1 String Methods
* 3.6.2 String Formatting Operations
* 3.6.4 Mutable Sequence Types

from the "library reference" part and

* 5.14 Summary

with the list of operator precedence from the "language reference" part.



STEP 2:

Try some of the easier golfing challenges on codegolf.com:

* 99 Bottles of Beer
* Choose
* Roman to Decimal
* Grid Computing
* Vigenere Cipher
* Prime Factors
* Pascal's Triangle
* Bob Ross' The Joy of ASCII Art



STEP 3:

* Read more about code golfing and look for golfing tips & tricks. Try googling for codegolf. This forum is a good place to start, and so is Mark Byers'codegolfing tips site.
* Learn about functional programming in python with filter(), reduce() and especially map() and lambda functions.
* Read about the more advanced built-ins, like zip(), and take note of the keyword arguments for those which have them, such as sorted(), min(), max().
* Learn how to encode and embed data efficiently in the source by using int(), hex(), oct() and above all, 8bit ASCII with ord()!

Note: To have ASCII 0-31 ("control characters") in your source code, insert them with your regular Python editor if it is supported, or use a hex editor. Note that ASCII 0, 10 and 13 requires the two byte escapes \0, \n and \r respectively. ASCII character code 26 (SUB) does not work in all windows versions. This is a bug, and has something to do with ASCII 26 signifying DOS end-of-file character. To use ASCII 128-255 you have to use a 3 byte BOM (byte-order mark) at the very start of the source code file. The bytes are 0xEF, 0xBB, 0xBF. Either save the file with UTF-8 as encoding, or put them there with a hex editor. See section 4.8.2 Encodings and Unicode in the documentation.



STEP 4:
Try some of the advanced golfs on codegolf.com:

* Reverse
* Switchboard
* Musical Score
* Numeric Diamonds
* Total Triangles
* Saving Time
* Home On The Range
* Seven-Segment Displays



STEP 5:
Join #codegolf on irc.freenode.net to discuss golfing matters with other golfing adepts and addicts.
Have a look at the mind-numbing GolfScript by flagitious, take note of the examples, especially the wonderful algorithm for 1,000 digits of PI.
Read the Great Tome of Golfic Knowledge, and read all posts tagged with golf over at the Perl Monastery. Although they are zealous heretics, their golfing knowledge is old and runs deep.

Then try some of the more challenging codegolf.com problems:

* 1,000 Digits Of Pi
* Oblongular Number Spirals
* Brainfuck
* Tower of Hanoi
* Calendar
* Dancing Queens



STEP 6:
Join #anagol on irc.freenode.net and start doing challenges over at Shinh's golfserver. Make sure that you try some of the older challenges, some of which are always open.
Read the best solutions and learn the dirty tricks of their creators.



STEP 7:
With the newfound knowledge and tricks learnt from your golf research and the dissection of winning entries over at shinh's golf-addict hangout, it's time to try to reach for the top. This requires cooking up devilish clever solutions to the most horrendous problems here on codegolf.com:

* Paint By Numbers
* Polynomial Division
* Conway's Game of Life
* Crossword
* SHA-256 Hashing


As you follow these steps, you will percolate steadily upwards through the ranking list, until you reach your nadir; hopefully in the top 10, but below myself...


Thanks to tryeng for getting me hooked on codegolf, helping me get started and constantly being a few points ahead of me on the top list ;-)

Add a reply