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.
Sign up for your weekly dose of what's up in emerging technology.
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.
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
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):
Code Golf- while foo(a):print a;a*=2
- Replacing Append
Code Golf- A+=B,
- Ceil Value of a Real Number
Original- from math import ceil
n = 3/2
Code Golf- n = 3/2
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
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
- 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
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.