So I've become seriously addicted to code golf. For those that don't know, code golf is a competition where you try to pass some programming test with a program consisting of the least number of characters. I chose to play code golf in Python because I wanted to learn more about the language. I'll probably eventually do the same for Ruby.
I know the title of this might be controversial. There's alot of people who have never tried it, know no one who has, but still have strong opinions on why golfing is a negative practice to undertake. These people are wrong. Golfing is just a programming puzzle. It doesn't remove your ability to recognize ugly code. Anyone who tries to "golf" outside of a golfing context has bigger problems.
What follows is a brief guide to the idioms that I learned through code golf that could be useful in real programming. It will also serve as a helpful set of tips for python golfers. I'll probably do a followup post with idioms that are useful but are completely worthless for any real programming.
I am by no means a code golf expert. I recently attained the 6th rank and have 5th well within striking distance, but my average per "hole" isn't staggering. So please keep in mind this is by no means a definitive list of techniques required to be successful. If anyone wants to email me some tips on the Dancing Queens puzzle, feel free. I've been beating my head against a wall on this one for a few weeks and haven't even gotten close to Mark Byer's 100. I don't know Mark, but I'd like to shake his hand.
if a: # before bob() jim() if a: bob();jim() # after
Simple. Effective. One line if/while/for statements shouldn't be used in regular python very often but there are times when removing that white space can sometimes substantially clean up the code.
if a<0: # beginner python b=2*a else: b=3*a b=2*a if a<0 else 3*a # proper python b=a<0 and 2*a or 3*a # codegolf1 b=a*(3,2)[a<0] # codegolf2
Now, all of the above idioms have pros and cons as far both function and readability. Let's get the caveats out of the way. The first code golf technique above only works if your first expression doesn't evaluate to 0. The second code golf expression doesn't short circuit (so, for example,
(0,a/b)[b!=0] does not prevent division by 0).
All three of these idioms (if/else, and/or, and the index into a tuple) are worth understanding and recognizing. They can all be very powerful in certain real world situations.
print "Good Morning!" + (mailcount>0)" You have new mail!" print "You have %d email message%s."%(mailcount, 's'(mailcount != 1))
a=b=0 # I had no idea python let you do this transpose=zip(*list) rev = anything[::-1] # reversed() that works on any sequence print
y# convert to string (via repr) print x, # ends print with a space (instead of a newline)