Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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 ?

is a competition to solve a particular problem in the fewest bytes (or characters) of source code.

What are the requirements of ?

There should be a complete, unambiguous specification of the problem. It must include at least:

  1. How should the answer be presented – should a bare function be acceptable, or it must include full input/output code?
  2. The input format.
  3. The output format.
  4. A clear description of how the input and output are related.
  5. Explicit statement of any error checking or recovery that should be performed if any (because fragile solutions are par for the course)
  6. Example inputs and corresponding outputs.
  7. 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:

What makes a good ?

  1. Thoroughly specified.
  2. Simple enough to be understood fully in at most two quick readings of the spec.
  3. Admit rapid naive implementations.
  4. Complex enough to admit more than one reasonable way to accomplish it.
  5. Have built-in-to-some-languages solutions excluded (i.e. no eval for arithmetic parsers and the like).
  6. Not be optimized for one language or one class of languages.
  7. Not be too closely related to others we've done recently.
  8. Solve a class of problems rather than a single instance.

How should I answer a ? 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 char in 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 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.
history | excerpt history