Skip to content

Instantly share code, notes, and snippets.

@cky
Created November 18, 2013 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cky/7533352 to your computer and use it in GitHub Desktop.
Save cky/7533352 to your computer and use it in GitHub Desktop.
Stack Overflow [code-golf] tag info

Code-golf is a competition to solve a particular problem in the fewest bytes of source code.

  • The question must be Community-wiki. Please flag for moderator attention to make it so.

  • 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 (because fragile solutions are, well, par for the course)
    6. Example inputs and outputs.
       
  • You, the asker, should be able to answer the code-golf problem (even if it's not optimal), to show that it is not a homework problem. If possible, post your solution as an answer.

  • All popular programming languages should be allowed. (Counter example)

  • Code-golf and programming puzzles are trying to find a new home here

What makes a good code-golf?

A good code-golf should

  1. be thoroughly specified
  2. be simple enough to be understood fully in two quick readings of the spec
  3. admit rapid naive implementations
  4. be 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 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 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 much less bytes.
  • Many times, a more inefficient algorithm can reduce the code size. (Example)

Things to note

  • The language you use should not be invented just for solving this code golf. Solutions like this are no longer funny.
  • Avoid undefined, unspecified or implementation-defined behavior.
  • If you use Unicode, byte count should use UTF-8.
  • Try to explain your solution.
  • You should show how the program or function is executed, accepting the input, and printing the output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment