People who code: we want your input. Take the Survey

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
11
votes
2answers
522 views

What is a good way to deal with tasks that require arrays using Haskell?

Often a task requires real arrays. Take for instance the task to implement Befunge or ><>. I tried to use the Array module for this, but it's really cumbersome, ...
14
votes
31answers
2k views

Mathematical Combination

Write a program that takes an input such as: n,k which then computes: $$\binom n k = \frac {n!} {k!(n-k)!}$$ and then prints the result. A numerical example: ...
12
votes
5answers
1k views

Polynomial Interpolation

Write a program that performs Polynomial Interpolation using true arbitrary precision rational numbers. The input looks like this: f(1) = 2/3 f(2) = 4/5 f(3) = 6/7 ... You may assume that there's ...
12
votes
8answers
800 views

Finding maximum paths

Given a square of positive, natural numbers write a program find a horizontal and a vertical path with the sum of numbers along them being maximal. A horizontal path goes from the first column to the ...
18
votes
15answers
3k views

The Time Traveler

The shortest code to generate the correct times on the clocks wins. You are a seasoned time traveler and have been known to stop at many planets during your journies. Each planet rotates at a ...
14
votes
12answers
1k views

An Old Irish Blessing

I don't know who coined these words, and I'm not Irish, but I give you an Irish blessing: ...
9
votes
5answers
704 views

A subset of Verbal arithmetic

Implement a verbal arithmetic solver of several same sequence of numbers added together: ...
16
votes
7answers
3k views

Decompose a permutation into cycles

There is a well-known theorem that any permutation can be decomposed into a set of cycles. Your job is to write the shortest possible program to do so. Input: Two lines. The first contains a ...
12
votes
9answers
2k views

Forking Factorials

This golf requires a factorial calculation be split up among multiple threads or processes. Some languages make this easier to coordinate than others, so it is lang agnostic. Ungolfed example code is ...
22
votes
5answers
2k views

Visualize long division with ASCII art

Write a program that visualizes long division with ASCII art. Input consists of two integers, a numerator and a denominator, using the input format of your choice. Examples: 1234 ÷ 56: <...
9
votes
2answers
979 views

Floating point addition, without floats!

Your task is to write a program, in any language, that adds two floating point numbers together WITHOUT using any fractional or floating point maths. Integer maths is allowed. Format The format for ...
25
votes
15answers
2k views

Langton's ant ASCII art.

Draw the path of Langton's ant. Description Squares on a plane are colored variously either black or white. We arbitrarily identify one square as the "ant". The ant can travel in any of the four ...
22
votes
9answers
4k views

Interpret ><> (Fish)

While ><> is not a popular language, it can be good for golfing and has been used on this website. It was inspired by Befunge and has some similarities in its instructions. Required Commands: <...
10
votes
1answer
5k views

Build a simple proxy

Make the shortest proxy server. Input/Output Specifications Client: Input: a port number (32-bit integer) (,) or (space) a url (see test cases) Output: html source of the url Server: Input: port ...
13
votes
13answers
2k views

Compute the first N digits of e

Challenge Write a program to compute the the first N (<= 10^3) digits of e. Your program should take an integer N as input. Input: 100 Output: ...
13
votes
20answers
1k views

Uptime Progress Bar

Write a program which parses the output of uptime and generates an anatomically suggestive progress bar (as shown) with a length equal to the current uptime in days:...
8
votes
4answers
414 views

The fourth power (Juzuk)

In 1939 Juzuk described a way to generate the fourth powers of natural numbers. Group the natural numbers like this: ...
81
votes
101answers
13k views

Hello World 0.0!

source: Dilbert, September 8, 1992 I'm hoping to add a new twist on the classic "Hello World!" program. Code a program that outputs Hello World! without: String/...
32
votes
14answers
2k views

Draw a Suanpan abacus

Write the shortest program that takes a single integer as input and prints out a Suanpan abacus Testcases Input: 314159 Output: ...
8
votes
2answers
650 views

Simple Geometric problem

This problem (see below) has been given as a High School Programming League code golf challenge. The shortest codes submitted during the contest were: 177 bytes in Ruby, 212 bytes in Python 2.5, 265 ...
16
votes
9answers
3k views

Calculate digits of Pi

This is a somewhat different task. Calculate 1024 hexadecimal digits of π, beginning at the 1024th hexadecimal place. Formally: Your program should complete in less then 1 minute and produce the ...
18
votes
4answers
603 views

Permutation group operation

There is a well-known bijection between the permutations of \$n\$ elements and the numbers \$0\$ to \$n!-1\$ such that the lexicographic ordering of the permutations and the corresponding numbers is ...
29
votes
36answers
12k views

Determine if 4 points form a square

Write a function that takes 4 points on the plane as input and returns true iff the 4 points form a square. The points will have integral coordinates with absolute values < 1000. You may use any ...
47
votes
78answers
8k views

Answer to life, the universe, and everything

Task Given a String as input, your task is to output 42 only if the input String happens to be exactly the following : ...
12
votes
8answers
1k views

Quine Challenge I

Challenge In this task you have to write a program that will take input an integer N (-1e9 <= N < 0 && 0 < N <= +1e9), then compute T = (abs(N) % M+1),if N is positive ...
8
votes
2answers
665 views

GolfScript - The missing function

The GolfScript language has one serious lack, no float or fixed point handling. Remedy this by creating a function F for converting an integer number, given as a string, to a string representing 1 / ...
20
votes
46answers
2k views

Detect rotated strings

Read two strings from stdin. Output Yes if one string is a rotated version of the other. Otherwise output ...
13
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) ...
6
votes
8answers
808 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
636 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). ...
86
votes
138answers
12k 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 ...
20
votes
54answers
4k views

Chessboard pattern

Input: a positive number \$n\$, smaller than \$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 ...
3
votes
1answer
366 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.
19
votes
5answers
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 ...
5
votes
5answers
342 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
339 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). ...
11
votes
7answers
4k 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
930 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 ...
11
votes
15answers
3k 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: Wikimedia Commons) Each column is a binary-coded digit, reading hours, minutes and seconds ...
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
692 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]. ...
11
votes
16answers
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 $$\mathfrak{U} = \...
11
votes
14answers
1k 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. ...
13
votes
15answers
1k 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. ...
27
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
431 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
386 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
383 views

Mixed up output [duplicate]

Write a function or program that outputs the following lines <input> time: ...
19
votes
5answers
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 ...