ArticleslgStudy

science

Code golf

Code golf is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Code golf rather than just read about it. In short: 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. Code golf challenges and tournaments may also be named with the programming language used (for example, Perl golf).

Key takeaways

  • Code golf belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Code golf to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Code golf from memory before moving on to harder problems.

Reference excerpt

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. Code golf challenges and tournaments may also be named with the programming language used (for example, Perl golf).

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 (or keystrokes, to extend the metaphor) 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, and later popularised through the use of Perl to write a program that performed RSA encryption, 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. 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 programming languages have been created specifically for the practice of code golf. These languages sacrifice readability for extreme brevity, and often provide high-level functions with single-character names, which makes them unsuitable in production environments where maintainability has higher importance than code size.

History The first recorded dedicated golfing language is GolfScript, released in 2007, with a derivative called floGScript releasing the next year. In 2014, CJam, heavily inspired by GolfScript, released. in 2015, motivated by CJam and APL, and inspired by J, Jelly released, and would position itself as a dominant contender for tersest language for many years. Since 2014, many dedicated golfing languages have been released. Notable examples include Japt, Pyth, 05AB1E, Charcoal and Vyxal.

Design Some golfing languages, such as Japt and Pyth, are transpiled to conventional programming languages (here, JavaScript and Python respectively). This makes the languages easier to learn, and guarantees a solid foundation for the design and performance of the language. This also simplifies the creation of web interpreters, which allow users to use the language without downloading anything. Many modern golfing languages, being inspired by or related to golfscript, are stack-based and concatenative, but several languages inspired by Jelly, itself tacit and functional, instead follow its footsteps. Other paradigms, such as pattern matching languages, also appear in less notable quantities. Most golfing languages are high-level, and almost all shorten symbols to a single character. In modern implementations, such as in Vyxal, the languages leverage single-byte character sets to make all commands printable, which further reduces readability due to the variety of symbols, especially with the incompatibility of code comments with the goal of code golf. Golfing languages almost always make heavy use of operator overloading, and most include easily-accessible forms of text compression. In most languages, closing parenthesizes and other syntactic structures is optional at the end of the program. As a general design principle, dedicated golfing languages are heavily implicit, and most are tacit.

Examples An example of GolfScript code to print 1000 digits of π:

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

An example of Vyxal 3 code for Fizz buzz, annotated with an explanation:

⑤ƛ35f≛kF½•∨,­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁣⁣‏‏​⁡⁠⁡‌⁢⁣​‎‎⁡⁠⁣⁤‏‏​⁡⁠⁡‌­ ⑤ƛ ## ‎⁡map over 1..100 35f ## ‎⁢does 3 or 5... ≛ ## ‎⁣divide the number? result is 2-wide bitmask kF½ ## ‎⁤["Fizz", "Buzz"] • ## ‎⁢⁡multiply previous by bitmask, then concatenate ∨ ## ‎⁢⁢if previous is falsey (empty string), push implicit n , ## ‎⁢⁣print

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.

Practical applications

In 2019, cryptography researchers Xavier Bonnetain, Léo Perrin and Shizhu Tian used code golf and the closely linked concept of Kolmogorov complexity to show that the S-box used in the Streebog and Kuznyechik cryptography algorithms was significantly less random than its creators claimed it was.

See also

Code poetry Data compression Minification (programming) Obfuscation (software) Perl pastimes Kolmogorov complexity

References

External links CodeGolf.StackExchange.com: Questions and answers on programming puzzles and code golf Golf on the esoteric programming languages wiki List of dedicated golfing languages List of all languages used 10 times or more on code golf stack exchange, ranked by brevity

Worked examples

Example 1 — a first encounter with Code golf

Start with the simplest possible case. Write down what Code golf claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Code golf before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Code golf ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Code golf

In research
Code golf appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Code golf in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Code golf is common in secondary-school and first-year university syllabi. It links to neighbouring topics Programming contests, so understanding it makes those chapters shorter.
In everyday life
Look for Code golf outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Code golf” →

How to study Code golf in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Code golf means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Code golf out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Code golf in simple terms?

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. Code golf challenges and tournaments may also be named with the programming language used (for example, Perl golf).

Why does Code golf matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Code golf?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Code golf.

Tags

  • Programming contests

Keep exploring