Questions tagged [code-golf]

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

10,430 questions
Filter by
Sorted by
Tagged with
2k views

Solving triangles with trigonometry

Time to dig up your old trigonometry notes from high school! The challenge is to solve the unknown sides and angles of different triangles. And as is customary in code golf, the smallest working code ...
2k views

Machine Epsilon

Machine epsilon is an important floating point number to know when doing numerical calculations. One way to understand it is when this relation ...
2k views

Meta Golf Challenge

On that challenge you have to solve that challenge. Contest is over! Check the end of the question Specs: Write the smallest code (Any language*). The score of an answer is the sum of: Code length ...
2k views

Bridges and Tunnels

You are responsible for building a new freeway. However, it leads through mountainous lands and therefore needs numerous bridges and tunnels. The freeway itself should stay at a single level. Input ...
2k views

Knuth's Power Tree

In The Art of Computer Programming, Knuth describes an algorithm to compute short addition chains. The algorithm works by computing a tree known as the Power Tree. The tree is rooted at 1, and each ...
880 views

Unicode character classification [closed]

Real-world programs often use libraries like ICU to support Unicode. The puzzle is to create the smallest possible function that can determine whether a given Unicode character is lowercase, ...
4k views

Magic square generator

Create the shortest function to print a magic square of given size. A magic square is a NxN matrix of distinct numbers and the sum of each row, column and diagonal is equal to a constant. A magic ...
2k views

Undulant numbers [duplicate]

An undulant number is a number where its digits alternate between up and down like the following number: 461902 or 708143, or even 1010101, but not 123, because 2 < 3. Write a program or function ...
380 views

Negative Regexp [closed]

Write the shortest function that takes one string, and returns a regular expression that matches anything EXCEPT the input string, in parenthesis. Escape necessary characters like Perl ...
816 views

Soundex function

Write the shortest function to generate the American Soundex code for a surname only containing the uppercase letters A-Z. Your function must produce output consistent with all the linked page's ...
2k views

Facial Recognition

The aim of this task is to identify, clean up, and mark out all the faces in any given 'image'. What's in a face? A face will be a ZxZ square where Z is an odd integer greater than 1. The top left ...
503 views

using a Markov Chain, because this question itself as input text for example

Create a Markov Chain generator, which generates (and outputs) an Order 4 text chain. It must be able to support all plain text formatting used on this question, so be sure to check for punctuation (...
10k views

Generate a mandelbrot fractal [closed]

Your task is to draw the mandelbrot set in ascii. It should look something like The complex number c lies in the mandelbrot set, when the sequence ...
768 views

Another amicable number problem

Two numbers are said to be 'amicable' or 'friends' if the sum of the proper divisors of the first is equal to the second, and viceversa. For example, the proper divisors of 220 are: 1, 2, 4, 5, 10, ...
1k views

Paint that fence

You are Tom Sawyer and you have to paint a fence of 102400 m long. Luckily, your friends decided to help you in exchange of various things. Each friend will paint L meters, starting from S with ...
3k views

Indent a string using given parentheses

Given the following input to the program: List of block start characters List of block end characters A string to format format the string with the blocks delimited by the two character sets ...
454 views

Add two vectors modulo 5

Interpret a triple of 32-bit integers as a vector of 32 integers modulo 5. Write an addition routine for these vectors. Example: ...
1k views

Find largest sum of subsequence

Given a sequence of integers find the largest sum of a subsequence (integers on consecutive positions) of the sequence. The subsequence can be empty (in which case the sum is 0). Input is read from ...
442 views

Implement strategy for Jacks or Better Video

You are dealt five cards from a single deck. Implement a strategy for Jacks or Better Video Poker that has an expected revenue of at least 99.46%. Output the cards you want to hold. The shortest code ...
4k views

The strange life of a beehive

Researchers recently discovered an interesting bee colony that lives in an infinite field of honeycomb: Each cell can house a bee or not. In fact, the lives of those creatures appear to be a ...
3k views

Generating guitar tabs?

Write the shortest program that generates guitar tabs for the chords given as input. So that the guitarists among you don't have an advantage, and to make it deterministic (and probably easier to ...
5k views

Check if number is a sum of consecutive numbers or not

Write a program that checks if a given positive integer can be represented as sum of two or more consecutive positive integers. Example: 43 can be represented as 21 + 22 10 = 1+2+3+4 but 4 cannot be ...
6k views

Write a self-replicating program.

Write a simple program that copies itself when executed. Your program should be some kind of executable file on Windows, Linux, etc.., should generate new executable file, which is identical to your ...
9k views

Generate a random program in your favorite language [closed]

We all heard of testing compilers using randomly generated inputs. Your task is to write a program to generate a valid program (including no undefined behavior) in your favorite language. The ...
543 views

Pinyin Combinations

Create a function that takes a string of a pinyin syllable as the argument, and returns true of the combination exists, false otherwise. Use "v" for "ü". Here is a full list of combinations. http://...
9k views

Tips for golfing in PHP

What general tips do you have for golfing in PHP? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to PHP (e.g. "remove comments" is not ...
1k views

Compute the difference between two days.

Another date-manipulating problem :P Task Write a program or a function that calculates the difference between two dates given by a user. Input & Output Similar to the previous one, inputs ...
935 views

Jumping and Running

Matthew likes solving puzzles. Whenever he manages to solve one he skips around happily. Recently he really needs to do this as a meteor shower has opened craters and holes in the ground in which he ...
1k views

Shortest code for Seven-Eleven

The Seven-Eleven problem is find 4 positive numbers (with two decimal digits, A.BC) such that their sum is equal to their product and is equal to 7.11. ...
1k views

Tips For Golfing In Groovy

As I have seen many questions asking tips for golfing in their interested language, I decided to ask for my favorite language: Groovy. People can give some tips and tricks that will be used in golfing ...
1k views

What's the day today (or other dates)?

Task Write a program or a function that calculates a week-day name of a date which a user inputs. Input & Output Input is a string, YYYYMMDD. Example of ...
7k views

Shortest, lexicographically smallest generating string

A string x generates a string y if y is a substring of an infinite repeat of ...
456 views

Daily horoscope for zodiac signs [closed]

Code a daily horoscope for the 12 zodiac signs, it needs to be self-sustained (no external ressources), quite randomized but fixed by zodiac sign for each day. It needs to extract three "variables" ...
879 views

Tips for golfing in Clojure

What are your tips for code golfing using Clojure? The aim of this question is to collect a list of techniques that are specific to Clojure and can be used in general code-golfing problems.
16k views

Shortest way to reverse a number [closed]

Write a function (or equivalent subprogram) to accept a single integer valued argument and return a (similarly typed) value found by reversing the order of the base-10 digits of the argument. For ...
3k views

Make a simple IRC bot.

Task Make a simple IRC bot which does simple task like greeting user. Detail (You may see this RFC document.) Write a complete program that perform these: The program inputs ...
726 views

Calculate the number of topologies on {1,2,…,n}

Task Write a function/program which takes n as a parameter/input and prints/returns the number of topologies (which is demonstrated below) on the set ...
4k views

Code Golf: 6174 - Kaprekar's mythical constant

Why is the number 6174 so interesting? As defined by Wikipedia Take any four-digit number, using at least two different digits. (Leading zeros are allowed.) Arrange the digits in ascending ...
773 views

Score counter for Skat

You task is to write a small program, that counts the points of a Skat hand. A Skat deck has cards 7 to 10, Jack, Queen, King and Ace (called Unter, Ober, König and Daus). We use the German suits, ...
351 views

Reversing base numbers [closed]

Where input is binary, convert it to a decimal number (base 10), reverse the decimal number and output it. Vice versa for decimal (base 10) input: reverse it, convert it to binary and output the ...
1k views

Code 39 Barcode Scanner

Code 39, developed in 1974, is one of the most commonly used symbologies, or types, of barcodes, although it is the UPC/EAN system that is most often seen in retail sales. Code 39 barcodes can encode ...
350 views

Expanding Expressions

Given an expression of one-letter variables ([a-z]), operators (*, +, &) and parenthesis, expand it using the following ...
1k views

Pascal's Pyramid and Higher Dimensions

Pascal's Triangle is a familiar mathematical construct with many interesting properties. It is constructed by starting with a 1 on top, and generating the numbers in the next row from the sum of the ...
10k views

Implement Sleep Sort

Sleep Sort is an integer sorting algorithm I found on the Internet. It opens an output stream, and for each input numbers in parallel, delay for the number seconds and output that number. Because of ...
906 views

Square Number Digit Density

The square number digit density (SNDD) of a number - invented by myself - is the ratio of the count of square numbers found in consecutive digits to the length of the number. For instance, 169 is a 3-...
1k views

Egyptian Pyramids

The Great Pyramid of Giza, the largest pyramid in Egypt, is not only the oldest of the Seven Wonders of the Ancient World, but it is also the only one to remain largely intact. The Egyptian Pyramids ...
53k views

Tips for golfing in JavaScript

What general tips do you have for golfing in JavaScript? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to JavaScript (e.g. "remove ...
3k views

The minimum fibonacci challenge!

Challenge In this task you would be given an integer N (less than 106), find the minimum way in which you could sum to N using only Fibonacci numbers - this partition is called Zeckendorf ...