Welcome to the Code Golf wiki!
This is a community-curated collection of tips, tricks, suggestions, and reference information for golfing in the many languages and holes on code.golf.
- Language-agnostic tips for the beginner golfer are collected below; you might also check out other golfing sites for additional resources and holes to practice on.
- Tips for certain puzzling or challenging holes can be found on the hole-specific tips page.
- The constant holes (e, π, √2, etc.) utilize spigot algorithms in many languages.
- Each language has its own page for commonly known tricks and strategies, linked in the sidebar.
If you'd like to contribute to or tidy up a page, head over to the GitHub wiki to make edits. The links in the upper right of each page will also take you there.
Please aim to contribute tips which are generally applicable, with sufficient detail and explanation for a new golfer to see how it might be used. You are under no obligation to divulge your more precious secrets, but if many golfers have found a trick, it may be worth adding here.
General Tips
- Remove comments, as they won't be removed for you.
- Remove as much whitespace as possible; your language's parser may surprise you in what it can handle!
- Remove trailing semicolons where possible, as they can be omitted in many languages.
- Many languages can avoid verbose
if
statements. Alternatives include- Short-circuiting operators, such as
&&
and||
in C-family languages.if(x>10)x++
->x>10&&x++
- Ternary operator
?:
in C-family languages.if(x>10)y=1;else y=2;
->y=x>10?1:2
- Array lookups:
if x > 10: 1 else 2
->[2,1][x>10]
- Short-circuiting operators, such as
- Re-use variables (no matter how inappropriate it may seem!) to reduce code spent on initialization.
- Likewise, use short names for your variables and functions, including aliasing built-in functions you use often.
- In multiple languages,
~-x
and-~x
can be used as alternatives tox-1
andx+1
. This saves no bytes on its own, but can save a pair of parentheses when used in a larger expression. - Try replacing logical operators
&&
and/or||
with bitwise operators&
and/or|
, respectively. There are many cases where it can save a byte for each replacement. - Study your language's operator precedence so that you can remove as many parentheses as possible.
Strategies
- Regular expressions can be extremely powerful tools for string manipulation if your language supports them natively.
- Arbitrary precision integers, a.k.a. "bigints", can be used like a list of digits in some base, which may prove shorter for certain computations.
- Strings can also be used as a list of digits. Sometimes string operations (including regular expressions) are shorter than the equivalent list-of-numbers operations.
- Condensing two loops into one can be very useful, especially in languages with C-like loops (e.g. Java, PHP). You can look to primo's PHP post on this for more information. This trick usually requires some creativity to make sure certain parts of your code only run when you want it to, which can be difficult when trying to only use one loop to save bytes.
- For looking up data, consider using an array (or string, if possible) indexed by a hash function. For example, if the problem requires the number
x
to map to a certain word, one could use something like["golf","is","fun"][x*4%5%3]
. Finding the best hash function may require some combination of creativity and "brute force" (i.e., testing lots of stuff and see what works).
Good introductory holes
Although the difficulty can vary a lot by language, these are some holes that are often a good starting place to build your skills. Don't be frustrated if you can't reach 1st place, though; even simple holes can have devious tricks that require experience to find.
- The Tutorial provides practice with printing, looping, and accessing arguments.
- Fibonacci, Niven Numbers, and Leap Years are easier numerical sequences.
- Fizz Buzz has some simple math and string operations.
- The most tied golds page contains holes with easier solutions, or solutions that are spoiled elsewhere on the internet. (Note that code.golf asks you don't share or spoil your solutions--this is a competitive golfing site).
Maximizing your score
There are many ways to enjoy the problems on code.golf. Some people like to "fill in the grid"--that is, solve every hole in every language--even if not all solutions are competitive. Others prefer to solve fewer holes but make every effort to find the best solution. And some people like to maximize their leaderboard score.
The scoring system is described here. The fastest way to accumulate points is to solve as many holes as possible, even if your solutions aren't that great. Once you've solved every hole in at least one language, further points require optimizing your solutions. The only way to get the full 1000 points for a hole is to find the shortest solution across all languages.