Default Rules: Code Golf Answer Types
When answering a code-golf challenge, what types of answer are acceptable by default? Can an answer be a complete program, a function, an anonymous function, a snippet of code?
Please add one type per answer. An answer can express that a particular type is acceptable, or that it is not acceptable. Since voting up and down on answers will indicate community consensus, edge cases for particular languages or unusual circumstances can be added as separate answers.
A snippet is not a valid answe …
3mo ago
Anonymous function A code gol …
3mo ago
Function Score is the bytes o …
3mo ago
Complete program A code golf …
3mo ago
4 answers
Complete program
A code golf answer can be a complete program. This means that the code can be interpreted or compiled (as appropriate) without any additional source code.
0 comment threads
Function
Score is the bytes of the function
A code golf answer can be a function. Only the bytes required to define the function are included in the score.
Score includes imports and definitions
If the function depends on additional source code in order to work, the bytes required for that additional source code are also included in the score. For example, if a library needs to be imported, or a constant needs to be defined, doing so before the definition of the function does not avoid it being included in the score.
Python example code: Fibonacci
PHI=(1+5**.5)/2
def f(n):return round(PHI**n/5**.5)
for n in range(10):
print(f(n))
Here the for
loop is only used for printing output for testing, and is not required for the function to work, so its bytes are not included in the score. However, if PHI
is not defined, the function will not work, so the bytes to define PHI
are included in the score, even though it is defined outside the function.
The answer would therefore be posted as
PHI=(1+5**.5)/2
def f(n):return round(PHI**n/5**.5)
In practice this could also be posted as
def f(n):PHI=(1+5**.5)/2;return round(PHI**n/5**.5)
The point is that extracting the definition outside the function does not avoid the definition being counted towards the score.
Python example code: Area of circle
from math import pi
def a(r):return pi*r*r
The function will not work without from math import pi
, so it must be included in the score. The answer would therefore be posted as shown, or could also have the import included inside the function
def a(r):from math import pi;return pi*r*r
Either way the score is the same.
0 comment threads
A snippet is not a valid answer
A code golf answer must in general be able to take input[1] and give output. A snippet of code that is not contained in a function or program cannot take input[2], instead requiring the input value to be inserted directly into the code. This is not an acceptable type of answer, with the exception of programming languages for which there is no other way to take input.
This reflects the insertion into the source code rule from Default Rules: Code Golf I/O.
-
Some challenges require no input, such as some random output challenges or kolmogorov-complexity challenges. In such cases I would expect the "snippet" to work as a complete program, so these don't seem applicable here. ↩︎
-
If a "snippet" takes input from STDIN, I would expect it to work as a complete program, so that doesn't seem applicable here. ↩︎
Anonymous function
A code golf answer can be an anonymous function[1]. That is, a function that can be defined without giving it a name (in programming languages that allow this).
If an anonymous function does not call itself, it need not be given a name. If an anonymous function is recursive, it needs a name in order to call itself so the bytes required to name it count towards the score.
Python example code: Fibonacci
If a non-recursive approach is used, no function name is required:
lambda n:round(((1+5**.5)/2)**n/5**.5)
If a recursive approach is used, the function must have a name to call:
f=lambda n:f(n-1)+f(n-2)if n>1else n
In this particular case, the recursive approach is shorter, even with the additional 2 bytes for f=
.
Reasoning
The reason for not requiring a name is that in general an anonymous function can be defined and called directly, without ever naming it.
outputs=map(lambda n:round(((1+5**.5)/2)**n/5**.5),range(10))
for o in outputs:
print(o)
-
The additional rules in the function answer apply equally to anonymous functions. ↩︎
0 comment threads