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
8 votes
1 answer
854 views

The smallest Chess arbiter

Write a program, which, upon gazing at a chess table, can say whether there is a check or a checkmate. Input: a chess table in text form, the last line of the input being the first rank (the starting ...
user avatar
  • 8,090
12 votes
6 answers
849 views

Bijection between binary strings and pairs thereof

Input: Either one or two strings consisting of '0's and '1's. If there are 2, they are separated by a space. All strings are of ...
user avatar
9 votes
7 answers
1k views

Output a shuffled deck using random input

Input/Output: Input: A uniformly random, infinitely long, string of '0's and '1's, taken from stdin. The string is assumed to be truly random, not pseudo-random. It is uniform in that each ...
user avatar
9 votes
1 answer
739 views

Compress data with context free grammars

It is possible to compress some kinds of data, such as human text or source code, with straight-line grammars. You basically create a grammar whose language has exactly one word – the uncompressed ...
user avatar
  • 9,960
15 votes
9 answers
758 views

Tips for golfing in Postscript?

As one of the less popular languages, it's difficult to find literature on the avant garde of postscript hackery. So what discoveries have the golfers here made to exploit the stack model (or other ...
user avatar
  • 4,827
5 votes
1 answer
354 views

Determine the original sequence

Write a program that will determine the original sequence given five variations. The original sequence will be contain n unique elements in any order. A variation is made by making a copy of the ...
user avatar
  • 16.4k
4 votes
13 answers
973 views

Find The Least Common Characters

Given a string, find the 10 least common alpha-numeric characters within that string. Output should be formatted with numbers left-padded with spaces so they look like this: ...
user avatar
  • 4,194
6 votes
11 answers
916 views

Ode-cay Olf-gay

Write a program which accepts a sentence into stdin, and writes that sentence to stdout in pig latin. Here are some test cases which your program should handle: ...
user avatar
  • 1,195
16 votes
8 answers
2k views

Turing Machine Simulator

Write a Turing machine simulator. For simplicity we can assume statuses as integer, symbols as char, blank symbol equals whitespace 5-tuple in the form of current state, input symbol, next state, ...
user avatar
22 votes
12 answers
8k views

Find the Pisano Period

The Fibonacci sequence is a well know sequence in which each entry is the sum of the previous two and the first two entries are 1. If we take the modulo of each term by a constant the sequence will ...
user avatar
34 votes
6 answers
12k views

Balance chemical equations!

Bernd is a high school student who has some problems in chemistry. In class he has to design chemical equations for some experiments they are doing, such as the combustion of heptane ...
user avatar
  • 9,960
3 votes
2 answers
354 views

Finding limits of functions

A limit in math determines the value that a function f(x) approaches as x gets closer and closer to a certain value. Let me use ...
user avatar
  • 4,194
2 votes
5 answers
498 views

Perform basic operations on complex numbers in a language without native support for complex numbers

Your code has to provide the following functions: read(x) Reads a complex number from the standard input. It has to accept and evaluate something in the form ...
user avatar
  • 8,090
12 votes
3 answers
431 views

Regions of regular polygons

Given a regular N-gon with all diagonals drawn, how many regions do the diagonals form? For example, a regular triangle has exactly 1, a square has exactly 4, pentagon has exactly 11, and a hexagon ...
user avatar
  • 1,677
91 votes
124 answers
18k views

Print this diamond

This question has been spreading like a virus in my office. There are quite a variety of approaches: Print the following: ...
user avatar
  • 1,019
9 votes
5 answers
520 views

Partitions of a list

The answer to this question is much too long Your challenge is to write a partitioning function in the smallest number of characters. Input example ...
user avatar
  • 14.9k
12 votes
27 answers
2k views

Removing unique elements from string

I came upon this question, because it seems to be very common use-case to find unique characters in string. But what if we want to get rid of them? Input contains only lower case alphabets. Only ...
user avatar
  • 239
15 votes
15 answers
2k views

Golf the Subset-Sum Problem

Task Given a list of space-delimited integers as input, output all unique non-empty subsets of these numbers that each subset sums to 0. Test Case Input: ...
user avatar
  • 2,162
5 votes
3 answers
48k views

Shortest code to check if a number is in a range in JavaScript

This is how I checkout to see if a number is in a range (in between two other numbers): ...
user avatar
  • 775
6 votes
4 answers
554 views

Determine number of connected components

You are given a graph represented through an adjacency list. For example, the input 1-2 2-3 3-4 4-1 would correspond to the graph with nodes 1, 2, 3 and 4, where ...
user avatar
  • 2,162
5 votes
4 answers
3k views

The shortest code to tell if a number is even or odd

One of my colleagues proposed us a challenge: to write the shortest C/C++ program to determine if a number is even or odd. These are the requirements: Get a number from keyboard, at run-time (not ...
user avatar
24 votes
10 answers
3k views

Score a game of Bowling

Your task is to sum up and output one player's score in a game of 10-pin bowling after up to 21 rolls. The rolls are represented as a sequence of integers in your preferred method of input. Each ...
user avatar
  • 18.1k
6 votes
10 answers
597 views

Print integers in any base up to 36 [duplicate]

Possible Duplicate: Integer to string with given radix This question is inspired by Pythons builtin int function. int is ...
user avatar
  • 1,007
2 votes
3 answers
822 views

m3ph1st0s's programming puzzle 4 (C/C++): “3-way swap” [closed]

My last puzzle has generated some confusion and controversy so I decided to give up, for now, on those "replace one character"-type puzzles. Hope this 4th puzzle will redeem myself after all the ...
user avatar
11 votes
21 answers
975 views

Return each number from a group of numbers

The challenge The program must return all numbers included into a group (comma and hyphen separated sequence) of numbers. Rules s is the sequence string; all ...
user avatar
10 votes
12 answers
1k views

Dice from Changing Random Generator

Introduction You are given a random integer generator with the following implementation The first invocation always returns 1. The second invocation returns a random integer between 1 and 2. The ...
user avatar
  • 5,952
17 votes
10 answers
530 views

Ascending matrix

The "ascending matrix" is an infinite matrix of whole numbers (0 included) in which any element is the smallest available element which has not been previously used on the respective row and column: <...
user avatar
15 votes
2 answers
559 views

The Major-Minor Dichotomy

Given a list of chords label them as either 'Major' or ' Minor'. Input Input will be a list of chords, one per line, made up of 3 notes separated by a space. Each note will consist of the note name ...
user avatar
  • 11.2k
14 votes
8 answers
948 views

Code Golf Bingo!

You are given a Bingo board and a list of calls. You must print BINGO! as soon as your board wins the game. Bingo boards look like this: They will be specified like this: ...
user avatar
12 votes
6 answers
1k views

Score a game of Yahtzee

For each of the 13 rows of a Yahtzee scoresheet you are given (from stdin) a space separated list of 5 numbers (dice). Your task is to calculate the score for each line and output the Grand Total of ...
user avatar
  • 18.1k
13 votes
7 answers
1k views

Write all possible Braille characters

An interesting puzzle came to me looking at the elevator buttons this morning. You are required to generate a list of all Braille patterns that fit in a 2x3 grid. Use a hash ...
user avatar
  • 8,570
7 votes
2 answers
3k views

Quickly find length of n-th term of the look-and-say sequence

The look and say sequence is an example of a run length encoding sequence. To get the next term of the sequence one groups the sequence into runs of the same number, each group in the next term then ...
user avatar
53 votes
49 answers
6k views

Can you Golf Golf?

You are required to generate a random 18-hole golf course. Example output: [3 4 3 5 5 4 4 4 5 3 3 4 4 3 4 5 5 4] Rules: Your program must output a list of hole ...
user avatar
  • 1,243
10 votes
11 answers
1k views

Array Sandwiches

(Adapted from Problem C of the first qualifier of the ACM Programming Contest of 2012/2013) You have several arrays, named \$A_1, A_2, ... , A_n\$, each sorted in ascending order. Every item in the ...
user avatar
  • 4,194
6 votes
9 answers
5k views

matrix determinant [closed]

Calculate the determinant of an n x n matrix. Rules read matrix from standard input write to standard output do not use ready solutions from library or language input: columns separated by any space ...
user avatar
15 votes
21 answers
6k views

Calculate the nth term of Golomb's self-describing sequence

Inspired by the previous question. Golomb's self-describing sequence g(n) is a sequence where any natural number n is repeated within the sequence g(n) times. The ...
user avatar
  • 4,194
24 votes
11 answers
4k views

Generate a Kolakoski sequence [duplicate]

Definition1 A Kolakoski sequence is a self-describing infinite sequence {kn} of alternating blocks of 1's and 2's, given by the following rules: k0 = 1 kn = the length of the (n+1)'th ...
user avatar
  • 2,289
15 votes
4 answers
3k views

Create a GUI Piano [closed]

Challenge Create a GUI Keyboard with as few characters as possible. Example Because this was an assignment in one of my courses, I can't show the source code. However, here is a screen shot of my ...
user avatar
  • 1,447
5 votes
5 answers
811 views

Traverse all numbers with only one bit flip per step

You can count 0, 1, 2, 3, 4. But to get from 3 to 4 you have to change 3 bits at once. Given a number of bits, how can you go through all numbers once, but change at each step only 1! bit. Here is an ...
user avatar
  • 1,811
12 votes
5 answers
705 views

Visualise long multiplication with ASCII art

The challenge Write a program that takes two integers from standard input, separated by a comma, and then prints a visualisation of long multiplication of those two integers to standard output. Eg: ...
user avatar
  • 4,049
14 votes
28 answers
2k views

Shorten text with Run Length Encoding

Shorten (or not) text using run length encoding Input: heeeello woooorld Output: 1h4e2l1o 1w4o1r1l1d Read lines from stdin. Print to stdout. Stderr is of course discarded. Assume there are ...
user avatar
  • 1,488
-4 votes
38 answers
1k views

Given a number, program must return the alphabet letter correspondent

The program Given a number, program must return the alphabet letter correspondent. Rules The challenge here is to make it as short as possible, everyone knows it is a very easy program to do. The ...
user avatar
7 votes
8 answers
1k views

calculate days of a month [closed]

I would like to see the shortest expression to calculate the days (or the last day) of a month in any language. Considering y as the number of year and m as month. Basic rules without date / time ...
user avatar
7 votes
1 answer
505 views

Write a CFG acceptor

Write a program that, given a context free grammar and a string, returns whether the string is a word of the language defined by the grammar. Input Your program should read a context free grammar and ...
user avatar
  • 9,960
18 votes
6 answers
5k views

Recreate the classic snake Game

The challenge is to create the classic Snake game using as few bytes as possible. Here are the requirements: The game must be implemented in a typical 2-dimensional layout. The snake should be able ...
user avatar
  • 513
25 votes
3 answers
1k views

Solving Three Open Problems with a Halting Oracle

You are given the functions: h1(f,*args) and h2(f,*args) Both are methods which are already defined for you (here the asterisk indicates a variable number of arguments) f is a function, *args is a ...
user avatar
  • 935
1 vote
2 answers
957 views

A doubly linked list that only stores 1 pointer per element [closed]

A favorite puzzle of mine. Not massively difficult but a tad interesting. The Objective Using a low level language (C/C++/similar level) - create a doubly linked list of elements that only stores a ...
user avatar
  • 101
13 votes
5 answers
543 views

Given a fair coin as input, generate any particular unfair outcome

It is easy to generate a fair coin using a unfair coin, but the reverse is harder to accomplish. Your program will receive one number X (between 0 and 1, inclusive) as input. The input must not ...
user avatar
  • 28.5k
9 votes
2 answers
905 views

The GolfScript Meta Par Hole

Seem like GolfScript wins all of these. So you can't beat them, join them. Write an Self-Contained Golfscript Interpreter I'm using the definition of self-contained to mean, a single program:- So no ...
user avatar
  • 1,314
23 votes
6 answers
1k views

Lego gear ratios

I'm building a giant lego robot and I need to generate some particular gear ratios using a set of gears. I have lots of gears with the common lego gear sizes: 8, 16, 24, or 40 teeth. Write a program ...
user avatar

1
210 211
212
213 214
223