Code golf
Solve the problem in as few bytes as possible. Like golf, the lowest score wins.
●○○ easy · where to see it
The idea
Code golf is a game in which everyone solves the same small task, and the shortest program wins. It grew up in Perl circles in the late 1990s and found a permanent home on the Code Golf Stack Exchange site. Length is usually counted in bytes.
Read the story
Golf changes how you think. Readable names, clear structure and error handling all cost bytes, so they go. What is left is the bare shape of the computation, plus every trick the language allows.
Eventually people built languages for golf.
- GolfScript (Darren Smith, 2007). Stack-based, every built-in one character.
- Jelly (Dennis Mitchell, 2015). Its own 256-character code page, so each of its many built-ins is a single byte. Programs look like line noise and are often under ten bytes.
- HQ9+ (Cliff Biffle). The joke at the end of the road.
Hprints Hello World,Qprints the program (a quine),9prints the lyrics of "99 Bottles of Beer". Unbeatable at exactly three tasks, useless at all others.
HQ9+ shows the catch. A language can make any one task short by building it in. Golf languages win by choosing built-ins that make many tasks short. Behind the game sits a real idea, Kolmogorov complexity, the length of the shortest program that produces a given output. It is a measure of how much information the output contains, and it cannot in general be computed.
How it works
FizzBuzz (print 1 to 100, but Fizz for multiples of 3, Buzz for 5, FizzBuzz for both).
FizzBuzz (print 1 to 100, but Fizz for multiples of 3, Buzz for 5, FizzBuzz for both). A plain Python version is 183 bytes.
for i in range(1, 101):
if i % 15 == 0:
print("FizzBuzz")
elif i % 3 == 0:
print("Fizz")
elif i % 5 == 0:
print("Buzz")
else:
print(i)
The golfed version is 62 bytes and prints exactly the same 100 lines.
for i in range(1,101):print("Fizz"*(i%3<1)+"Buzz"*(i%5<1)or i)
The tricks, one at a time.
| Trick | Why it works |
|---|---|
i%3<1 instead of i%3==0 | one byte shorter; a remainder is never negative |
"Fizz"*(…) | True is 1 and False is 0, so this is "Fizz" or "" |
"Fizz"…+"Buzz"… | the 15 case falls out for free: "FizzBuzz" |
… or i | an empty string is false, so or falls back to the number |
| one line, no spaces | the loop body may follow the colon |
Golf is mostly about finding structure; the 15 case disappears because Fizz and Buzz combine. That is compression, not deleting spaces.
Seen in
- GolfScript. The first popular golf language.
- Jelly. A custom code page and hundreds of one-byte built-ins.
- HQ9+. The parody of golf by built-in.
- Brainfuck. A favourite golf target because it has no built-ins; every byte is earned.
Try this
- Run both Python versions and check that their output is identical.
- Shave the golfed FizzBuzz further. (Hint: loop over
range(100)and print-~i, which isi+1. 59 bytes is possible.) - Hard: write the shortest Brainfuck program you can that prints
Hello. Compare with others.