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

Questions tagged [code-golf]

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.

12
votes
1answer
3k views

Solve the 8 Puzzle

The 8 Puzzle is the smaller variant of the 15Puzzle (or the Sliding puzzle). You have a 3x3 grid which is filled with numbers from 0-8 (0 denotes the blank tile) ...
5
votes
8answers
790 views

Code Golf: Number of paths!

Challenge Consider a square matrix of order N(N rows and N columns). At each step you can move one step to the right or one step to the top. How many possibilities are to reach (N,N) from ...
8
votes
2answers
566 views

Turn 2D boolean array into (rectilinear) polygons

Challenge Write a program which, given a 2-dimensional boolean array (equivalently, a monochromatic bitmap), outputs a series of polygons that describe the outline of the region that is “true” (1). ...
83
votes
130answers
11k views

Determine whether strings are anagrams

Challenge Given two strings, work out if they both have exactly the same characters in them. Example Input word, wrdo This returns true because they are the ...
9
votes
27answers
2k views

Chessboard pattern

Input: a positive number, smaller then 80, from stdin or as a command-line argument. Output: A square chessboard pattern, the size of the input number. The dark fields are represented by the letter '...
3
votes
1answer
352 views

Check printer - Convert to English text from the input value.

Check printer function (or subroutine) - Convert to English text from the input value. 12.34 -> "twelve dollars and thirty four cents". For the range .01 to 9999.99 assuming valid input.
16
votes
4answers
2k views

ASCII art Bresenham line drawing

Write the shortest possible program that draws a Bresenham line in ASCII art. Your program should take two integers x and y (...
12
votes
12answers
2k views

Hitting 495 (Kaprekar) [duplicate]

Input: A three digit number from stdin or as an argument not all digits are the same may start with one or two zero's Process Order the input-digits ascending and descending to form two new ...
4
votes
4answers
295 views

Rotate Cartesian coordinates.

Write a program rotates some Cartesian coordinates through an angle about the origin (0.0,0.0). The angle and coordinates will be read from a single line of stdin in the following format: ...
-3
votes
3answers
334 views

Shorten this code while fixing a major bug [closed]

I found the code below today after much hair pulling. Shorten it down (code golf) and fix the bug that I found (bonus). ...
10
votes
7answers
3k views

Implement Shamir's Secret Sharing reconstruction

Shamir's secret sharing scheme is a simple way of protecting a secret by splitting it into several parts needed to reconstruct it. Your task is to implement Shamir's Secret Sharing reconstruction ...
10
votes
20answers
836 views

Calculate probability of getting half as many heads as coin tosses.

Write a program that, given a small positive even integer from standard input, calculates the probability that flipping that many coins will result in half as many heads. For example, given 2 coins ...
10
votes
12answers
2k views

Draw a BCD Binary clock

Create a BCD binary clock in ASCII. As implied, it shall display time in six columns of dots: (source: hypftier.de) Each column is a binary-coded digit, reading hours, minutes and seconds from ...
8
votes
8answers
2k views

Output a playable crossword grid

Write a program to produce a file containing a crossword grid that the user can print out and work the puzzle on. Input A filename representing a crossword grid file and optionally a second ...
4
votes
6answers
663 views

Write a program to perform set operations.

Your task is to write a program to perform set operations. The operations and arguments are taken from command line in the format program operation [argument]. ...
9
votes
14answers
1k views

Numbers, Sums, Products

Given a 20×20 grid of non-negative integers, find a 3×3 sub-grid where the product of the sums of the individual lines hits the maximum. Spoken in formulas: Given the 3×3 sub-grid the function to ...
10
votes
14answers
951 views

Percentage of work days in a month

Given a year and a month, find out the percentage of work days in said month. Work days are Monday through Friday with no regard to holidays or other special things. The Gregorian calendar is used. ...
12
votes
13answers
961 views

Make a number palindrome

Write a function that takes a number as an argument and makes it a palindrome by appending minimum number of digits. The number will be at max of 100 digits. ...
26
votes
19answers
4k views

My Word can beat up your Word

PROBLEM Given two words, find the winner in a digital root battle. Define the digital root of a word this way: Each letter of the alphabet is assigned a number: A = 1, B = 2, C = 3, ..., Z = 26 ...
8
votes
7answers
386 views

Shortest Port Scanner

Write the shortest program that will attempt to connect to open ports on a remote computer and check if they are open. (It's called a Port Scanner) Take input from command line arguments. ...
4
votes
2answers
379 views

Implement cdbmake

Write the shortest program to build a cdb database, using the following syntax for input: +klen,dlen:key->data followed by a newline. Example: ...
-3
votes
9answers
359 views

Mixed up output [duplicate]

Write a function or program that outputs the following lines <input> time: ...
17
votes
7answers
2k views

The Tic Tac Toe Dictionary

A TicTacToe game can be represented by a string denoting the sequence of positions as the players make their move. 0 1 2 3 4 5 6 7 8 Assume ...
9
votes
5answers
415 views

Golfed fixed point combinator

Write a fixed point combinator in as few characters as possible, in the language of your choice. free form (i.e., whatever's shortest): whole program, actual function, code snippet you may not use ...
7
votes
4answers
2k views

Noughts and Crosses (aka Tic-Tac-Toe)

Write a Notes and Crosses application. User interface is optional. You can output the computers move in text: eg 1x1 (meaning ...
18
votes
19answers
2k views

Run-Length Encoding

Write a program uses run-length encoding to shorten a list of non-negative integers it has to read in. You can assume the non-negative integers can fit in 32bit signed integers. Input Format The ...
13
votes
12answers
1k views

Day of the week of the next Feb 29th

Write a function which takes a date and returns the day of the week of the next February 29th after that date. The input is a string in the ISO Extended format: YYYY-MM-DD (e.g. May 27th, 2010 would ...
28
votes
29answers
4k views

Render “Digital Clock Style” Numbers

Write the shortest program which will take a string of numbers (of up to at least 20 length) as input, and display the output using the standard digital clock style numbers. For instance for input 81,...
10
votes
5answers
772 views

Huffman golfing [duplicate]

Write a filter that converts text from standard input to a representation of its Huffman tree on standard output. In as few characters as possible. you're free to consider newlines or not output ...
21
votes
6answers
3k views

Koch Snowflake - codegolf

The Koch snowflake (also known as the Koch star and Koch island) is a mathematical curve and one of the earliest fractal curves to have been described. It is based on the Koch curve, which appeared in ...
14
votes
20answers
2k views

Next Friday the 13th

What is the shortest program that can predict the next time Friday will fall on the 13th day of the month? Must be an complete functioning program (not just a function/subroutine) Must print out date ...
10
votes
5answers
813 views

Dissociated Press

http://en.wikipedia.org/wiki/Dissociated_press Dissociated Press is an algorithm that generates random text from an existing text. The algorithm starts by printing any N consecutive words (or ...
14
votes
20answers
1k views

Random dice tipping

In a standard dice (die) the numbers are arranged so that opposite faces add to seven. Write the shortest possible program in your preferred language which outputs a random throw followed by 9 random ...
14
votes
6answers
868 views

Print Triangle Wave of Numbers

Given the amplitude and period for a wave, print the wave. See sample output for more details. The total number of wave forms equals the period, and the height of each wave equals the amplitude. ...
4
votes
8answers
924 views

How many switches are ON

A class has N student and the class room has N switches (all turned OFF and numbered 1 to N). Each student has an enrollment number assigned from 1 to N. Each student serially (by number) toggles the ...
8
votes
3answers
407 views

Physics golf: inclined shooting

"And now for something, completely different." An angry bird is shot at an angle β to the horizontal at a speed u. The ground is steep, inclined at an angle α. Find the horizontal distance q that the ...
7
votes
9answers
3k views

Square free Numbers

Given a number N, write the shortest complete program to find the number of square-free integers below N. Sample Input 100000 Sample Output 60794 Constraints: ...
8
votes
4answers
837 views

Crossword grid verification

Validate a proposed crossword grid. Entries should be full programs that simply test a proposed grid to determine if it meets a set of conditions for making crossword solvers happy. Input The input ...
13
votes
6answers
860 views

Comparing powers.

Compare two numbers N1 = abc, N2 = def by constructing a function f(a,b,c,d,e,f) that: returns 1 if N1 > N2 returns -1 if N1 < N2 Note: You are not required to return any value for any other ...
5
votes
8answers
265 views

Power modulo golf. [closed]

Write a function f(a,b,c) that calculates a^b (mod c) in less than 10 seconds. a, b < 10^100 c < 10000. examples: ...
5
votes
7answers
784 views

Modulo code golf

Write a function f(a,b) that returns a mod b given the constraints: you can't use division in any way ...
3
votes
3answers
837 views

Finding “sub-palindromes” 2: subsequences.

The same task as Finding "sub-palindromes" but instead of finding substrings you must find subsequences that are not substrings. eg1: ...
18
votes
5answers
4k views

Print a Binary Tree

Inspired by a recent question on SO... Write a function to print a binary tree in the following format: 3 / \ 1 5 \ / \ 2 4 6 The output should ...
4
votes
3answers
557 views

Implement BCD Arithmetic

Write the shortest function you can that takes two binary-coded decimal integers and a character indicating an arithmetic operation to perform and return the result. Input Specification You will be ...
10
votes
4answers
1k views

Polynomial Long Division

Implement polynomial long division, an algorithm that divides two polynomials and gets the quotient and remainder: (12x^3 - 5x^2 + 3x - 1) / (x^2 - 5) = 12x - 5 R 63x - 26 In your programs, you will ...
9
votes
13answers
776 views

Holiday Gift Exchange

Another problem from our internal golfing...this one around the holidays last year. PROBLEM Andy, Barb, Carl, Didi, Earl, and Fran are buying gifts for each other. Draw names for a gift exchange. ...
6
votes
7answers
628 views

How many squares are guarded by chess queens?

Today marks the 15th anniversary of Garry Kasparov's defeat against Deep Blue. Speaking of chess... Input is a string that represents an 8x8 chess board containing only empty squares (...
7
votes
1answer
1k views

Random Sentence Generator

Write the shortest program you can in any language that reads a context-free grammar from and the number of sentences to produce from stdin, and generates that many ...
7
votes
8answers
492 views

Is this figure connected?

You are given a list of (x,y) grid coordinates. Your function should decide whether the figure obtained by coloring in the squares of the grid with the given ...
13
votes
2answers
2k views

Roman numeral converter function

Create the shortest function to convert a string of Roman numerals to an integer. The rules for each letter can be found at the Wikipedia page. Letters above 1,000 will have parentheses placed ...