code golf Dec 17, 2010

Code golf is a programming game. Players are presented with a problem such as "sort a list" or "find the largest triangular number smaller than X". They then submit programs which solve this problem, with the goal being to use the least number of keystrokes in typing the program. Players may submit any number of programs. Usually each programming language gets its own scoring category, so as not to give an advantage to anyone based on their preferences. A score is only counted if the program is verified to solve the problem (usually by a computer).

The game began as "perl golf" and was later generalized to include any language, and indeed languages were created for the sole purpose of code golf. Popular choices include perl, PHP, Python, and Ruby.

Its only relation to golf is that lower scores are better.

The largest code golf site online is probably codegolf.com.

See also: obfuscated perl, perl bowling, obfuscated C, obfuscated C contest, obfuscated C++ contest


Example, using sed:

The problem---collapse records spread out over multiple lines into a single line each.

Example input:

REC a
b
c
d
REC
x
y

Example output:

REC a b c d
REC x y

I submit: /^REC/{x;p;x;$p;h;d};H;x;s/\n/ /;$p;x;d