Code golf

Last updated

Code golf is a type of recreational computer programming competition in which participants strive to achieve the shortest possible source code that solves a certain problem. [1] [2] Code golf challenges and tournaments may also be named with the programming language used (for example, Perl golf).

Contents

Etymology

The term "code golf" is derived from the similarity of its goal with that of conventional golf, where participants seek to achieve the lowest possible score, rather than the highest, as is the standard in most sports and game scoring systems. While conventional golf players try to minimize the number of club strokes needed to complete the course, code golfers strive to reduce the number of characters necessary to write the program.

History

The length of the shortest possible program that produces a given output (in any fixed programming language) is known as the Kolmogorov complexity of the output, and its mathematical study dates to the work of Andrey Kolmogorov in 1963. Code golf, however, can be more general than this, as it often specifies a general input-output transformation that must be performed rather than asking for a single output with no input.

Whilst the term "code golf" was apparently first used in 1999 with Perl, [3] and later popularised through the use of Perl to write a program that performed RSA encryption, [4] a similar informal competition is known to have been popular with earlier APL hackers. The challenging nature of aggressively optimizing for program size has itself long been recognized; for example, a 1962 coding manual for Regnecentralen's GIER computer notes that "it is a time-consuming sport to code with the least possible number of instructions" and recommends against it for practical programming. [5] Today the term has grown to cover a wide variety of languages, which has even triggered the creation of dedicated golfing languages.

Dedicated golfing languages

Several new languages have been created specifically with code golfing in mind. Examples include GolfScript, Flogscript, Stuck, and Vyxal, which are Turing-complete languages that provide constructs for concisely expressing ideas in code. Because golfing languages compete for extreme brevity, their design sacrifices readability, which is important for practical production environments, and therefore they are often esoteric. Sometimes, however, a language is designed for a practical purpose but turns out to be suitable for code golf.

An example of GolfScript code to print 1000 digits of pi: [6]

;'' 6666,-2%{2+.2/@*\/10.3??2*+}* `1000<~\; 

This prints a string starting with "3141592653" followed by 990 more digits of pi.

Code golf websites include novel golfing languages created by users to win code golf challenges. Other popular languages include 05AB1E, Husk, Pyth, CJam and Jelly.

Types of code golf

Some code golf questions, such as those posed on general programming sites, may not require implementation in a specific programming language. However, this limits the style of problems that it is possible for the problem designers to pose (for example, by limiting the use of certain language features). In addition, the creation of such "open" questions has resulted in the design of code golf specific programming language dialects such as REBMU (a dialect of REBOL). Both online and live competitions may also include time limits.

See also

Related Research Articles

<span class="mw-page-title-main">Kolmogorov complexity</span> Measure of algorithmic complexity

In algorithmic information theory, the Kolmogorov complexity of an object, such as a piece of text, is the length of a shortest computer program that produces the object as output. It is a measure of the computational resources needed to specify the object, and is also known as algorithmic complexity, Solomonoff–Kolmogorov–Chaitin complexity, program-size complexity, descriptive complexity, or algorithmic entropy. It is named after Andrey Kolmogorov, who first published on the subject in 1963 and is a generalization of classical information theory.

Brainfuck is an esoteric programming language created in 1993 by Urban Müller.

In the computer science subfield of algorithmic information theory, a Chaitin constant or halting probability is a real number that, informally speaking, represents the probability that a randomly constructed program will halt. These numbers are formed from a construction due to Gregory Chaitin.

In computing, Common Gateway Interface (CGI) is an interface specification that enables web servers to execute an external program to process HTTP/S user requests.

A "Hello, World!" program is generally a simple computer program which outputs to the screen a message similar to "Hello, World!" while ignoring any user input. A small piece of code in most general-purpose programming languages, this program is used to illustrate a language's basic syntax. A "Hello, World!" program is often the first written by a student of a new programming language, but such a program can also be used as a sanity check to ensure that the computer software intended to compile or run source code is correctly installed, and that its operator understands how to use it.

<span class="mw-page-title-main">Quine (computing)</span> Self-replicating program

A quine is a computer program which takes no input and produces a copy of its own source code as its only output. The standard terms for these programs in the computability theory and computer science literature are "self-replicating programs", "self-reproducing programs", and "self-copying programs".

<span class="mw-page-title-main">Regular expression</span> Sequence of characters that forms a search pattern

A regular expression, sometimes referred to as rational expression, is a sequence of characters that specifies a match pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation. Regular expression techniques are developed in theoretical computer science and formal language theory.

sed Standard UNIX utility for editing streams of data

sed is a Unix utility that parses and transforms text, using a simple, compact programming language. It was developed from 1973 to 1974 by Lee E. McMahon of Bell Labs, and is available today for most operating systems. sed was based on the scripting features of the interactive editor ed and the earlier qed. It was one of the earliest tools to support regular expressions, and remains in use for text processing, most notably with the substitution command. Popular alternative tools for plaintext string manipulation and "stream editing" include AWK and Perl.

An esoteric programming language is a programming language designed to test the boundaries of computer programming language design, as a proof of concept, as software art, as a hacking interface to another language, or as a joke. The use of the word esoteric distinguishes them from languages that working developers use to write software. The creators of most esolangs do not intend them to be used for mainstream programming, although some esoteric features, such as visuospatial syntax, have inspired practical applications in the arts. Such languages are often popular among hackers and hobbyists.

A string literal or anonymous string is a literal for a string value in the source code of a computer program. Modern programming languages commonly use a quoted sequence of characters, formally "bracketed delimiters", as in x = "foo", where "foo" is a string literal with value foo. Methods such as escape sequences can be used to avoid the problem of delimiter collision and allow the delimiters to be embedded in a string. There are many alternate notations for specifying string literals especially in complicated cases. The exact notation depends on the programming language in question. Nevertheless, there are general guidelines that most modern programming languages follow.

<span class="mw-page-title-main">Theoretical computer science</span> Subfield of computer science and mathematics

Theoretical computer science (TCS) is a subset of general computer science and mathematics that focuses on mathematical aspects of computer science such as the theory of computation, formal language theory, the lambda calculus and type theory.

Non-English-based programming languages are programming languages that do not use keywords taken from or inspired by English vocabulary.

<span class="mw-page-title-main">Algorithmic probability</span>

In algorithmic information theory, algorithmic probability, also known as Solomonoff probability, is a mathematical method of assigning a prior probability to a given observation. It was invented by Ray Solomonoff in the 1960s. It is used in inductive inference theory and analyses of algorithms. In his general theory of inductive inference, Solomonoff uses the method together with Bayes' rule to obtain probabilities of prediction for an algorithm's future outputs.

<span class="mw-page-title-main">Doxygen</span> Free software for generating software documentation from source code

Doxygen is a documentation generator and static analysis tool for software source trees. When used as a documentation generator, Doxygen extracts information from specially-formatted comments within the code. When used for analysis, Doxygen uses its parse tree to generate diagrams and charts of the code structure. Doxygen can cross reference documentation and code, so that the reader of a document can easily refer to the actual code.

dc is a cross-platform reverse-Polish calculator which supports arbitrary-precision arithmetic. It was written by Lorinda Cherry and Robert Morris at Bell Labs. It is one of the oldest Unix utilities, preceding even the invention of the C programming language. Like other utilities of that vintage, it has a powerful set of features but terse syntax. Traditionally, the bc calculator program was implemented on top of dc.

In computer science, arbitrary-precision arithmetic, also called bignum arithmetic, multiple-precision arithmetic, or sometimes infinite-precision arithmetic, indicates that calculations are performed on numbers whose digits of precision are limited only by the available memory of the host system. This contrasts with the faster fixed-precision arithmetic found in most arithmetic logic unit (ALU) hardware, which typically offers between 8 and 64 bits of precision.

In some programming languages, eval, short for the English evaluate, is a function which evaluates a string as though it were an expression in the language, and returns a result; in others, it executes multiple lines of code as though they had been included instead of the line including the eval. The input to eval is not necessarily a string; it may be structured representation of code, such as an abstract syntax tree, or of special type such as code. The analog for a statement is exec, which executes a string as if it were a statement; in some languages, such as Python, both are present, while in other languages only one of either eval or exec is.

Plain Old Documentation (pod) is a lightweight markup language used to document the Perl programming language as well as Perl modules and programs.

Algorithmic information theory (AIT) is a branch of theoretical computer science that concerns itself with the relationship between computation and information of computably generated objects (as opposed to stochastically generated), such as strings or any other data structure. In other words, it is shown within algorithmic information theory that computational incompressibility "mimics" (except for a constant that only depends on the chosen universal programming language) the relations or inequalities found in information theory. According to Gregory Chaitin, it is "the result of putting Shannon's information theory and Turing's computability theory into a cocktail shaker and shaking vigorously."

OmniMark is a fourth-generation programming language used mostly in the publishing industry. It is currently a proprietary software product of Stilo International. As of July 2022, the most recent release of OmniMark was 11.0.

References

  1. Code Golf Stack Exchange. About code-golf. Retrieved 2021-12-21.
  2. "Introduction to Code-golf | ASSIST Software Romania" . Retrieved 2023-03-23.
  3. Greg Bacon (1999-05-28). "Re: Incrementing a value in a slice". Newsgroup:  comp.lang.perl.misc. Usenet:   7imnti$mjh$1@info2.uah.edu . Retrieved 2011-07-12.
  4. Back, Adam. "RSA in 5 lines of perl" . Retrieved 2011-01-10.
  5. Andersen, Christian; Gram, Christian (1962). Lærebog i Kodning for GIER (PDF). Vol. 1 (3 ed.). Copenhagen: Regnecentralen. p. 104. Retrieved 2020-05-16.
  6. "GolfScript Examples" . Retrieved 2023-03-23.