Code Golf is a game that is designed to let programmers show off their excellency in codes by solving problems in the least number of characters. The word “Golf” in code golfing refers to the popular game golf where two players compete with each other, and the one with the fewest club strokes wins.
Similar to the golf game, code golf is a competition where the winner achieves the specifications in the fewest keystrokes. It is basically a kind of recreational computer programming competition where the participants compete to achieve the shortest possible source code that implements a certain algorithm.
Code Golfing can be said as a classic playground for programmers where the main attempt is to solve a problem with the least number of characters. It is written in Go language, licensed under MIT and is available on GitHub.
Code Golf is free and supports various programming languages including Python, Haskell, JavaScript, Julia, Perl, Rust, Swift, Ruby and more. Currently, it provides a number of games including 12 days of Christmas, Abundant numbers, Fizz Buzz, 99 Bottles of Beer, Diamonds, Evil Numbers and much more. If you are playing for the first time, it has been suggested to start with a simple game, such as Fizz Buzz.
How Code Golfing Works
Below here, we listed general tips of Code Golf that are implemented in popular languages like
Python and others.
- Conditional Statement:
Original- if a<b:return a
else:return b
Code Golf- return(b,a)[a<b]
- AND Operators
Original- if a > 1 and b > 1 and 3 > a and 5 > b: foo()
Code Golf- if 3 > a > 1 < b < 5: foo()
- Multiple Statements
Original- while foo(a):
print a;a*=2
Code Golf- while foo(a):print a;a*=2
- Replacing Append
Original- A.append(B)
Code Golf- A+=B,
- Ceil Value of a Real Number
Original- from math import ceil
n = 3/2
print(ceil(n))
Code Golf- n = 3/2
print(-(-n//1))
How Does The Score Work?
The score of your solution is the count of the Unicode characters in your source code. This means both “A” (U+0041 Latin Capital Letter A) and “????” (U+1F609 Winking Face) cost the same despite the 1:4 ratio in byte count in UTF-8.
For each hole, the shortest solution is awarded 1,000 points, with the points decreasing in uniform decrements per rank. Your overall score is simply the sum of your points in each hole. Also, the execution time is limited to 5 seconds.
Pros and Cons of Code Golf
Pros
Code Golfing can be used for various purposes, such as-
- Practising programming with the help of Code Golf
- Sharpening skills in coding
- Learning to optimise codes
- Can be used as a learning tool and experience
- To think something out of the box
Cons
- The codes in Code Golf are often difficult to understand
- Unlike usual programming codes, the codes of Code Golf are merely impossible to be modified.
Other Similar Tournaments
The International Obfuscated C Code Contest
The International Obfuscated C Code Contest (abbreviated IOCCC) is a computer programming contest for the most creatively obfuscated C code. In this contest, the programmers are asked to write the most obscure and obfuscated C program within the rules, stress C compilers with unusual code, etc.
During the competition, the programmers usually write the smallest C compiler which is able to compile itself, avoid the commonly used constructs, avoid the C preprocessor and tricky statements such as “if”, “for”, “do”, “while” and other such.
Know more here.
js13kGames: Code golf for game devs
Js13kGames is an annual online JavaScript competition for HTML5 game developers that began in 2012. The annual js13kGames competition channels creativity by challenging aspiring game developers to create a game using just 13,312 bytes, zipped.
Some of the tricks that the programmers use in this competition include inlining all images, concatenate images, minimising the JS codes, zipping the final entries, and other such.
Know more here.