Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Meta

Default Rules: Code Golf Answer Types

+0
−0

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

4 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+0
−0

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.


  1. 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. ↩︎

  2. If a "snippet" takes input from STDIN, I would expect it to work as a complete program, so that doesn't seem applicable here. ↩︎

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Vague definition of "snippet" (1 comment)
+0
−0

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)

  1. The additional rules in the function answer apply equally to anonymous functions. ↩︎

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+0
−0

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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »