Questions tagged [code-golf]

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

Filter by
Sorted by
Tagged with
6 votes
3 answers
729 views

Word guessing game

A program should take an input on stdin, and guess what a word might be. It should run as a session. One example session is shown below. Characters are fed to it, with a * for a letter not revealed; ...
user avatar
  • 3,262
16 votes
5 answers
2k views

Interpret Befunge-93

The program will be provided as the first argument, stdin should be used for all read requests by the program and stdout for all write requests. It can be assumed that the program is less than 80 ...
user avatar
  • 2,021
11 votes
18 answers
764 views

Output nearby primes

Write a program which takes an input (which may or may not be prime), and lists the immediate prime following and preceding it. Example input: 1259 Example ...
user avatar
  • 3,262
29 votes
5 answers
2k views

Advanced Calculator

You must write a program that evaluates a string that would be entered into an advanced calculator. The program must accept input using stdin and output the correct answer. For languages that do no ...
user avatar
89 votes
203 answers
20k views

Find the Factorial!

Create the shortest program or function that finds the factorial of a non-negative integer. The factorial, represented with ! is defined as such $$n!:=\begin{...
user avatar
4 votes
5 answers
1k views

Tree traversal: Depth-first search

Write the shortest code that traverses a tree, depth-first. Input Any programmatical structure found in your language of choice: lists, tuples, queues etc. Output A list of "names" of the nodes in ...
user avatar
  • 221
51 votes
7 answers
5k views

Robot Finds Kitten

The challenge The shortest code by character count to help Robot find kitten in the fewest steps possible. Golfers, this is a time of crisis - Kitten gone missing and it's Robot's job to find it! ...
user avatar
  • 611
30 votes
8 answers
4k views

Self-Interpreting Interpreter

Based on a comment by George Edison to this question, write the smallest self-interpreting interpreter. You may use the language of your choosing. Empty languages do not count. Your program must be ...
user avatar
  • 2,022
25 votes
13 answers
6k views

Assembly Language Quine

Write the shortest possible assembly-language quine. Use any ISA you want, unless it has a print-quine instruction or equivalent. Examples include x86, MIPS, ...
user avatar
  • 2,022
21 votes
19 answers
4k views

Basic Calculator

You must write a program to evaluate a string that would be entered into a calculator. The program must accept input and output the correct answer. For languages that do not have standard input/...
user avatar
13 votes
5 answers
626 views

Perfect powers in more than one way?

Challenge Your task is to write a program or function which, given a positive integer \$N\$, finds all positive integers less than or equal to \$N\$ that can be expressed as a perfect power in more ...
user avatar
  • 4,453
20 votes
6 answers
1k views

Fix a broken random function

A friend has an add-on card in their computer which generates a perfectly random number from 1 to 5 inclusive. Unfortunately, they spilt cola on it somehow, and it now generates only 2's for all ...
user avatar
  • 3,262
14 votes
18 answers
722 views

Integer to string with given radix

Write the shortest function to convert an integer into a numeric representation given a radix between 2 and 62. e.g.: to_string(351837066319, 31) => "codegolf"
user avatar
3 votes
6 answers
432 views

Minimum of last k [closed]

Write a function that takes two arguments: a number k and a list l, and that returns another list ...
user avatar
  • 5,906
33 votes
16 answers
4k views

Calculate 500 digits of pi

Write a program to calculate the first 500 digits of pi, meeting the rules below: It must be less than 500 characters in length. It cannot include "pi", "math.pi" or similar pi constants, nor may it ...
user avatar
  • 3,262
7 votes
0 answers
775 views

Stable positive/negative separation [closed]

From SO. You are given an array with positive and negative integers. Write a function to change the elements order in the array such that negative integers are at the beginning, the positive integers ...
user avatar
  • 5,906
6 votes
11 answers
592 views

Find the largest value of power.

Some numbers can be represented as perfect powers of other numbers. A number x can be represented as x = base^power for some integer base and power. Given an integer x you have to find the largest ...
user avatar
  • 4,453
9 votes
10 answers
653 views

Find the number of x-digit numbers whose product of digits is N

Given two numbers \$N\$ and \$x\$, find the number of \$x\$-digit numbers whose product of digits is \$N\$ \$N <10^6\$ and \$x <12\$ ...
user avatar
  • 4,453
15 votes
28 answers
3k views

Implement division

Implement a division algorithm in your favourite language which handles integer division. It need only handle positive numbers - but bonus points if it handles negative and mixed-sign division, too. ...
user avatar
  • 3,262
8 votes
8 answers
440 views

Remainder When N divided by 2^M

Write the shortest code to find the remainder when an input number N is divided by 2^M. You cannot use /,* and % operator. ...
user avatar
  • 4,453
24 votes
20 answers
2k views

Chris Pine's "Deaf Grandma"

I'm a mentor at RubyLearning and one of the exercises we give to our students is the "Deaf Grandma" exercise from Chris Pine's book "Learn to Program". Here's the description: Write a Deaf Grandma ...
user avatar
14 votes
4 answers
4k views

Write a JSON validator [closed]

Write a program that determines whether its input is valid JSON. Input: ASCII text: [\x00-\x7F]* Note: if ASCII is problematic, feel free to use another encoding, ...
user avatar
  • 10.4k
37 votes
8 answers
4k views

Self-compiling compiler [closed]

This is based on a talk on compilers I listened to a while back, but I, unfortunately, can't remember when or where. Create the shortest compiler in any language that can compile itself. Target any ...
user avatar
  • 2,022
16 votes
12 answers
2k views

Implement glob Matcher

Implement a function of pattern and string to be matched, return true if the pattern matches the WHOLE string, otherwise false. Our glob pattern syntax is: ? ...
user avatar
  • 5,831
16 votes
4 answers
31k views

Shortest URL regex match in JavaScript [closed]

Create the shortest regular expression that will roughly match a URL in text when run in JavaScript Example: ...
user avatar
12 votes
8 answers
2k views

Build a nine-level word guessing game

The basics: You'll need to provide a nine-level word guessing game in the fewest characters possible (in your language of choice). The metrics: Provide a word-list (one word per line, seperated by a ...
user avatar
  • 1,659
40 votes
12 answers
8k views

Drawing a gradient in ASCII art

Task description: Write a program as short as possible to draw a radial gradient in ASCII art. The size of the output grid is predefined. The center point and the characters to be used for the ...
user avatar
  • 12.8k
14 votes
8 answers
787 views

Calculating areas.

The smallest code that gives the area between the curve p(x) = a0 + a1*x + a2*x2 + ..., the line y = 0, the line x = 0 and the line x = C (i.e. something like this: ) You can assume that p(x) >= 0 ...
user avatar
  • 5,438
9 votes
9 answers
863 views

Split a word into parts with equal scores

Assuming A=1, B=2... Z=26, and the value of a word is the sum of these letter values, it is possible to split some words into two pieces such that they have equal values. For example, "wordsplit" can ...
user avatar
  • 3,262
13 votes
10 answers
1k views

Forecast the weather

Write the shortest program to forecast the weather for the next 7 days The input (from stdin) is the weekday and the season The output (to stdout) is seven lines weekday and the temperature in ...
user avatar
  • 14.9k
29 votes
44 answers
3k views

Implement Bogosort

Is solving Sudoku too hard? Even the brute force version? Here's a coding exercise that's a little easier. I hope. :-P Write the shortest function to implement bogosort. In specific, your function ...
user avatar
23 votes
13 answers
5k views

Implement a Brute Force Sudoku Solver

Implement the shortest Sudoku solver using guessing. Since I have received a few request I have added this as an alternative question for those wishing to implement a brute force sudoku solver. ...
user avatar
9 votes
1 answer
2k views

Build an engine for a maze game

This is a follow up of Print a maze question. If you like this question, please add more maze generation algorithms ;). For this task you'll have to implement a game engine for one player who must ...
user avatar
  • 5,906
17 votes
48 answers
2k views

IPv4 Integer Conversion Function

Write the shortest function to convert an IP address into it's integer representation and output it as an integer. To change an IPv4 address to it's integer representation, the following calculation ...
user avatar
76 votes
53 answers
29k views

Tips for golfing in Ruby

What general tips can you give for golfing in Ruby? I'm looking for ideas that can be applied to code golf problems in general that are specific to Ruby. (For example, "Remove comments" would not be ...
user avatar
  • 1,247
27 votes
4 answers
3k views

Implement a Non-Guessing Sudoku Solver

Implement the shortest Sudoku solver. Sudoku Puzzle: ...
user avatar
30 votes
49 answers
6k views

Calculate ISBN-13 check digit

Write a function which, given the first 12 digits of an ISBN-13 code, will calculate the entire ISBN via calculating and appending an appropriate check digit. Your function's input is a string ...
user avatar
8 votes
4 answers
567 views

Binary search ("bisect") tool

Implement the binary search algorithm as used to identify the source code revision that breaks a computer software program. Your tool should take two arguments specifying the earliest and latest ...
user avatar
  • 5,557
9 votes
10 answers
2k views

Crossword numbering

Produce a program to correctly number a crossword grid. Input The input will be the name of a file representing the crossword grid. The input filename may be passed as an argument, on the standard ...
user avatar
131 votes
136 answers
42k views

Obfuscated Hello World

Create the shortest possible obfuscated program that displays the text "Hello World". In order to be considered an obfuscated program, it must meet at least two of the following requirements: Does ...
user avatar
15 votes
41 answers
5k views

Shortest Floor Function

Your task is to implement a floor function in as few bytes as possible. A floor function is a function that takes a real number and returns the largest integer less than or equal to the input. Your ...
user avatar
48 votes
11 answers
16k views

Write an interpreter for the untyped lambda calculus

The challenge is to write an interpreter for the untyped lambda calculus in as few characters as possible. We define the untyped lambda calculus as follows: Syntax There are the following three ...
user avatar
  • 1,769
30 votes
19 answers
13k views

Twinkle Twinkle Little Star

Write a program to play the popular English nursery rhyme. (This file is licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license. Attribution: Helix84 at the English language ...
user avatar
  • 5,557
63 votes
121 answers
10k views

Reverse stdin and place on stdout

Requirements: Take an input on stdin including new lines / carriage returns of unlimited length (only bounded by system memory; that is, there is no inherent limit in the program.) Output the reverse ...
user avatar
  • 3,262
44 votes
43 answers
10k views

Reverse Polish notation

You must evaluate a string written in Reverse Polish notation and output the result. The program must accept an input and return the output. For programming languages that do not have functions to ...
user avatar
18 votes
12 answers
861 views

Compute modular inverse

Given two positive numbers \$x\$ and \$n\$ with \$x<2^n\$, write the shortest possible function to compute \$x^{-1} \mod 2^n\$. In other words, find \$y\$ such that \$xy=1 \mod 2^n\$. Your ...
user avatar
17 votes
2 answers
1k views

Slot Machine Hacker

Problem: Slot Machine Hacker from Facebook Hacker Cup 2011 Round 1B Goal: Shortest code in your favourite language using stdin/stdout. You cannot assume ...
user avatar
  • 12.2k
15 votes
4 answers
956 views

Freely available books for code golfing? [closed]

Can you recommend any freely available books about code golfing in general and especially in Java and C++?
49 votes
49 answers
7k views

Tips for golfing in PowerShell

What general tips do you have for golfing in Windows PowerShell? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to PowerShell (e.g. "...
user avatar
  • 12.8k
33 votes
14 answers
19k views

Drawing a cube in ASCII art

Task description: Draw a cube in ASCII art in roughly a cabinet projection. Monospaced fonts often have characters that are about twice as high as they are wide. ...
user avatar
  • 12.8k