dark mode light mode Search Menu
Search

Code Golf

sterlic on Flickr

Learning to code is only one of several key steps to become a good coder. Learning how to debug or fix code is another key step. A less obvious skill is the ability to code efficiently.

And efficiently doesn’t mean the basics: commenting your code when needed and reducing the number of steps and functions wherever possible. Efficient coding also includes spending time to find ways to do things with much less code than the average programmer might realize.

I recall seeing the PHP code files for pMachine, a content management system and blogging tool, when the code was refactored into object oriented code. The sparseness of the code was like looking at a cathedral. Every bit was simple, obvious, and tight with lots of clean white space. There were no tall heavy chunks of code. Opening every file showed the same clean and carefully thought out code.

The ability to achieve this effect in your code takes lots of time and hard work. Probably you also have to think in ways useful to coding, for example, in terms of cause and effect.

How do you learn to be an efficient coder?

What is Code Golf?

Code golf is a fun way to gain some insights into how to write truly efficient code. As with golf, where the fewer strokes you have the better your score, with code golf the less lines and characters of code in your program the better your score.

Code golf is a series of programming challenges that require coders to be efficient. You write a program to solve the challenge. You also see the results of other coders, learn from their efforts, and improve your skills. It’s a fun way to participate with a community of programmers.

You’ll also gain insights into programming languages you might not know because some languages work better for code golf than others. You’ll learn in depth about the order of operations, for example.

This is a guess on my part but code golf probably is most useful at the beginner level. More complicated code golf problems use techniques that are subtle and possibly violate the norms for any programming language. They might teach you bad habits for real coding.

Simple problems, however, might show a programmer ways to accomplish tasks they had not considered or known. Code golf might teach them lots of ways to reduce the size of their code and become more efficient programmers.

Simple Code Golf Problems

Here are two example problems to solve with Python to give you an idea how code golf works.

Hello World!

The problem is truly simple: print “Hello World!” (without double quotes).

The solution:

print("Hello World!")

Sleep In

This more complicated problem, from the CodingBat.com site (see Learn More links below), shows how one solution in Python can be simplified into other solutions.

The problem: The parameter weekday is True if it is a weekday, and the parameter vacation is True if we are on vacation. We sleep in if it is not a weekday or we’re on vacation. Return True if we sleep in. Write a function in Python to tell us if we can sleep in.

The traditional solution in Python defines a function sleep_in to test the two parameters, weekday and vacation:

def sleep_in(weekday, vacation):
  if not weekday or vacation:
    return True
  else:
    return False

The simplified one line solution defines a function sleep_in to return a value if it is not a weekday or it is a vacation:

def sleep_in(weekday, vacation): return(not weekday or vacation)

This code golf problem uses the programming rules for True and False to determine if two combinations of parameters, weekday and vacation, output True or False. It also shows the potential limits of code golf by using one line. The one line syntax might not be considered optimal code in a Python program even if the code works.

Learn More

CodingBat

Probably a great place to start with Java and Python problems. Includes a box to type in your solution plus answers and link to help with relevant parts of each language.
http://codingbat.com/

Code Golf (Wikipedia)

http://en.wikipedia.org/wiki/Code_golf

Anarchy Golf: All Problems

Problems in many different languages. You can upload your solution for a score.
http://golf.shinh.org/all.rb

StackExchange Code Golf

These problems are considered off-topic but easily found with the code-golf tag.
http://stackoverflow.com/questions/tagged/code-golf
http://codegolf.stackexchange.com/

Reddit Code Golf

https://www.reddit.com/r/codegolf

What Code Golf Taught Me about Python

By Louis Brandi, provides some insights into the strengths and limits of code golf.
http://lbrandy.com/blog/2008/09/what-code-golf-taught-me-about-python/

Programming and Logic Puzzles

Beyond code golf are sites with lots of programming problems to test your skills and knowledge, for beginners to expert coders.
http://www.billthelizard.com/2009/06/programming-and-logic-puzzles.html

Squeezing a playable chess program into 487 bytes

And beyond programming problems to test your skills are big problems like coding entire applications in the smallest amount of code possible. Here’s a chess program written in 1-2 kilobytes (your average video game is tens of megabytes in size, if not bigger).
http://1kchess.an3.es/
http://arstechnica.com/gaming/2015/01/squeezing-a-playable-chess-program-into-487-bytes/