[go: nahoru, domu]

Jump to content

Code golf: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Undid revision 850852302 by 89.189.145.150 (talk)
 
(42 intermediate revisions by 33 users not shown)
Line 1: Line 1:
{{More citations needed|date=September 2021}}
'''Code golf''' is a type of recreational computer programming competition in which participants strive to achieve the shortest possible [[source code]] that implements a certain [[algorithm]]. Playing code golf is known as "golf scripting". Code golf tournaments may also be named with the programming language used (for example ''[[Perl]] golf'').
{{short description|Recreational computer programming competition}}
'''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.<ref>Code Golf Stack Exchange. [https://codegolf.stackexchange.com/tags/code-golf/info About code-golf]. Retrieved 2021-12-21.</ref><ref>{{Cite web|url=https://assist-software.net/blog/introduction-code-golf|title=Introduction to Code-golf {{!}} ASSIST Software Romania|access-date=2023-03-23}}</ref> 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 to write the program.


== History ==
== History ==
Line 5: Line 10:
Whilst the term "code golf" was apparently first used in 1999 with [[Perl]],<ref name='perl-golf-coined'>{{cite newsgroup | author = Greg Bacon | title = Re: Incrementing a value in a slice |message-id= 7imnti$mjh$1@info2.uah.edu | newsgroup = comp.lang.perl.misc | date = 1999-05-28 | url = https://groups.google.com/group/comp.lang.perl.misc/msg/7b97c434492c8d20 | accessdate = 2011-07-12}}</ref> and later popularised through the use of Perl to write a program that performed [[RSA (algorithm)|RSA]] encryption,<ref name='rsa'>
Whilst the term "code golf" was apparently first used in 1999 with [[Perl]],<ref name='perl-golf-coined'>{{cite newsgroup | author = Greg Bacon | title = Re: Incrementing a value in a slice |message-id= 7imnti$mjh$1@info2.uah.edu | newsgroup = comp.lang.perl.misc | date = 1999-05-28 | url = https://groups.google.com/group/comp.lang.perl.misc/msg/7b97c434492c8d20 | accessdate = 2011-07-12}}</ref> and later popularised through the use of Perl to write a program that performed [[RSA (algorithm)|RSA]] encryption,<ref name='rsa'>
{{cite web | url = http://www.cypherspace.org/rsa/pureperl.html | title = RSA in 5 lines of perl | accessdate = 2011-01-10 | last = Back | first = Adam}}</ref> similar informal competition was known to have been popular with earlier [[APL (programming language)|APL]] hackers. Today the term has grown to be applied to a wide variety of languages, which has even triggered the creation of dedicated golfing languages.
{{cite web | url = http://www.cypherspace.org/rsa/pureperl.html | title = RSA in 5 lines of perl | accessdate = 2011-01-10 | last = Back | first = Adam}}</ref> a similar informal competition is known to have been popular with earlier [[APL (programming language)|APL]] hackers. The challenging nature of aggressively optimizing for program size has itself long been recognized; for example, a 1962 coding manual for [[Regnecentralen|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.<ref name="GIER">{{cite book |last=Andersen |first1=Christian |last2=Gram |first2=Christian |url=http://datamuseum.dk/w/images/b/be/LaerebogI.pdf |format=PDF |title=Lærebog i Kodning for GIER |volume=1 |edition=3 |location=Copenhagen |publisher=Regnecentralen |year=1962 |page=104 |accessdate=2020-05-16 }}</ref> Today the term has grown to cover a wide variety of languages, which has even triggered the creation of dedicated golfing languages.

== 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 are trying to minimize the number of club strokes needed to complete the course, code golfers are striving to reduce the number of key strokes necessary to write the program.


== Dedicated golfing languages ==
== Dedicated golfing languages ==
Several new languages have been created specifically with code golfing in mind. Examples include GolfScript and Flogscript, which are [[Turing completeness|Turing-complete]] languages which 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 programming languages|esoteric]]. Sometimes, however, a language is designed for a practical purpose, but turns out to be suitable for code golf. An example is [[binary lambda calculus]], designed to make [[algorithmic information theory]] more concrete, but providing a scheme suitable for code golf.{{citation needed|date=April 2016}}
Several new languages have been created specifically with code golfing in mind. Examples include GolfScript, Flogscript, Stuck, and Vyxal, which are [[Turing completeness|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 programming languages|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]]:
An example of GolfScript code to print 1000 digits of [[pi]]:<ref>{{Cite web|url=http://www.golfscript.com/golfscript/examples.html|title=GolfScript Examples|access-date=2023-03-23}}</ref>
<source lang="text">
<syntaxhighlight lang="text">
;''
;''
6666,-2%{2+.2/@*\/10.3??2*+}*
6666,-2%{2+.2/@*\/10.3??2*+}*
`1000<~\;
`50<~\; #truncate for webpage purposes
</syntaxhighlight>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.
-> 3141592653589793238462643383279502884197169399375
</source>

Code golf websites have users constantly creating new and unique golfing languages to win code golf challenges.


== Types of code golf ==
== 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.
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 ==
Other questions may be "King Of The Hill" challenges, scoring is based on the program's score competing in some form of game (Like reaching the top of a hill or collecting coins). These challenges usually competitive between the programs, with all of them playing the game at the same time, or in groups, in the same playspace.


*[[Code poetry]]
== See also ==
*[[Data compression]]
*[[Data compression]]
*[[Minification (programming)]]
*[[Perl#Perl pastimes|Perl pastimes]]
*[[Perl#Perl pastimes|Perl pastimes]]
*[[Code poetry]]
*[[Kolmogorov complexity]]
*[[Kata (programming)]]

== External links ==
*[https://stackoverflow.com/questions/tagged/code-golf StackOverflow.com: Questions tagged with 'code-golf']
*[http://codegolf.stackexchange.com CodeGolf.StackExchange.com: Questions and answers on programming puzzles and code golf]
*[http://regex.alf.nu/ regular expression golfing: a fun variant to code golf]
*[http://c2.com/cgi/wiki?ShortestWikiContest ShortestWikiContest]
*[https://esolangs.org/wiki/Category:Golfing_language List of dedicated golfing languages]</ref>


== References ==
== References ==
{{reflist}}
{{reflist}}

== External links ==
*[https://codegolf.stackexchange.com CodeGolf.StackExchange.com: Questions and answers on programming puzzles and code golf]
*[https://esolangs.org/wiki/Golf Golf] on the esoteric programming languages wiki
*[https://esolangs.org/wiki/Category:Golfing_language List of dedicated golfing languages]


[[Category:Programming contests]]
[[Category:Programming contests]]

Latest revision as of 22:58, 4 May 2024

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).

Etymology[edit]

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[edit]

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[edit]

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<~\;

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[edit]

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[edit]

References[edit]

  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". Newsgroupcomp.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.

External links[edit]