Code-golf is a competition to solve a particular problem in the fewest bytes of source code. If you want to score by characters instead of bytes, please state this explicitly in the challenge. If source code length is not the primary scoring criterion, consider using another tag instead.
What is code-golf?
code-golf is a competition to solve a particular problem in the fewest bytes (or characters) of source code.
What are the requirements of code-golf?
There should be a complete, unambiguous specification of the problem. It must include at least:
- How should the answer be presented – should a bare function be acceptable, or it must include full input/output code?
- The input format.
- The output format.
- A clear description of how the input and output are related.
- Explicit statement of any error checking or recovery that should be performed if any (because fragile solutions are par for the course)
- Example inputs and corresponding outputs.
- If the scoring is by characters rather than bytes, a statement to that effect.
All popular programming languages should usually be allowed unless there are exceptional circumstances outside of personal opinion.
If 1. and 7. are not specified, the following defaults are used:
- Answers are scored in bytes.
- Answers may be either full programs or functions (or equivalent).
- Functions do not have to be named, unless you require them to be named.
- Both programs and functions may take input via STDIN or command-line argument or prompt. Functions may also use their function arguments for input. Both programs and functions may output by writing to STDOUT. Functions may also use their arguments or return value(s) for output. These defaults are based on community consensus.
What makes a good code-golf?
- Thoroughly specified.
- Simple enough to be understood fully in at most two quick readings of the spec.
- Admit rapid naive implementations.
- Complex enough to admit more than one reasonable way to accomplish it.
- Have built-in-to-some-languages solutions excluded (i.e. no
evalfor arithmetic parsers and the like).
- Not be optimized for one language or one class of languages.
- Not be too closely related to others we've done recently.
- Solve a class of problems rather than a single instance.
How should I answer a code-golf? Any hints?
Pick your favorite language, solve the problem, and squeeze the size as much as you can! Most of the time you'll need to use discouraged idioms. The standard approaches are:
- Remove unnecessary spaces and new lines.
- Use one-letter variables instead of descriptive names.
- Always remember that a boolean can be implicitly converted to a 0 or 1 in many languages, and you can perform arithmetic on a
charin C and C++.
- Study the language and the built-in libraries thoroughly. There are often “hidden” features that can solve the problem in fewer bytes.
- Many times, a more inefficient algorithm can reduce the code size. (Example)
- Go through the appropriate tips question for your language to see if any tricks there can reduce your character count.
Things to note
- Unless the question is specified to be scored by characters, it is scored by bytes. If it doesn't specify a character encoding to use for scoring, answers which use Unicode code points outside 0 to 255 should state the encoding used.
- It is accepted practice not to count as part of the score compiler flags or any other non-standard marks required to get the interpreter/compiler to do what you need it to do.
- Try to explain your solution.
- You should show how the program or function is executed, accepting the input, and printing the output.