Questions tagged [code-golf]
Code-golf is a competition to solve a particular problem in the fewest bytes of source code.
11,543
questions
16
votes
40
answers
3k
views
Implement the `<=>` three-way comparison operator on numbers
In C++, there exists a a <=> b three-way comparison operator that, for numerical types, does the following:
If a < b, ...
83
votes
184
answers
9k
views
Output the sign
Given a number N, output the sign of N:
If N is positive, output 1
If N is negative, output -1
If N is 0, output 0
N will be an integer within the representable range of integers in your chosen ...
5
votes
2
answers
254
views
Shortening this Code to process nested tuple even further without the use of max()
I have the code:
...
47
votes
91
answers
5k
views
Division and remainder
This challenge, while probably trivial in most "standard" languages, is addressed to those languages which are so esoteric, low-level, and/or difficult to use that are very rarely seen on this site. ...
12
votes
29
answers
1k
views
All non-ordered pairs between the elements of an array
Task:
Return an array with all possible pairs between the elements of an array.
Example
From a=["a", "b", "c", "d"]; return ...
58
votes
47
answers
5k
views
How many steps does it take from n to 1 by subtracting the greatest divisor?
Inspired by this question over at Mathematics.
The Problem
Let n be a natural number ≥ 2. Take the biggest divisor of ...
11
votes
9
answers
1k
views
Great Leap to the Right
Given a string s consisting of characters a-z lowercase, generate 5 arrays a1, a2, ...
2
votes
4
answers
312
views
A randomly colored walk
Given two inputs, a distance \$d\$ and a number \$n\$ output a list of \$n\$ random colors which each have distance \$d\$ from the previous.
Background
A random walk is a path which is defined by ...
142
votes
558
answers
45k
views
Shortest infinite loop producing no output
Your task is to create the shortest infinite loop!
The point of this challenge is to create an infinite loop producing no output, unlike its possible duplicate. The reason to this is because the code ...
32
votes
29
answers
2k
views
Converge to a number
Your challenge is to, given a positive integer n, count up to each digit of it, giving the effect of converging on it.
Basically, count up to the first digit of n by its place value (\$⌊\log_{10}\left(...
29
votes
44
answers
4k
views
Visible Dice Faces
A traditional Western die is a cube, on which the integers 1 to 6 are marked on the faces. Pairs that add to 7 are placed on opposite faces.
As it is cube, we can only see between 1 and 3 faces (...
16
votes
7
answers
2k
views
We're gonna need a bigger podium!
If \$R\$ runners were to run a race, in how many orders could they finish such that exactly \$T\$ runners tie?
Challenge
Given a positive integer \$R\$ and a non-negative integer \$0\leq T\leq {R}\$ ...
170
votes
471
answers
34k
views
Implement a Truth-Machine
A truth-machine (credits goes to this guy for coming up with it) is a very simple program designed to demonstrate the I/O and control flow of a language. Here's what a truth-machine does:
Gets a ...
48
votes
40
answers
3k
views
At least h with at least h
Input
A list of nonnegative integers.
Output
The largest nonnegative integer h such that at least h of the numbers in the ...
81
votes
81
answers
8k
views
Print some JSON
This challenge is straightforward, but hopefully, there are plenty of avenues you can approach it:
You need to print/return a valid JSON object of at least 15 characters, not counting unessential ...
13
votes
51
answers
2k
views
Output a string's cumulative slope
Challenge
Given a string such as Hello World!, break it down into its character values: ...
148
votes
321
answers
41k
views
Fibonacci function or sequence
The Fibonacci sequence is a sequence of numbers, where every number in the sequence is the sum of the two numbers preceding it. The first two numbers in the sequence are both 1. Here are the first ...
26
votes
66
answers
6k
views
Minimum excluded number
This is intended to be an easy, bite-size code-golf.
The mex (minimal excluded number) of a finite collection of numbers is the smallest non-negative integer ...
67
votes
134
answers
11k
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 ...
25
votes
24
answers
2k
views
Hunt for discount
Story
My local pizza delivery introduced new discount. You get 50% discount from every second item on your order.
But being greedy capitalists, they forgot to mention that they will rearrange items ...
29
votes
54
answers
3k
views
Create a checkerboard matrix
Take a positive integer n as input, and output a n-by-n checkerboard matrix consisting of 1 and 0.
The top left digit should always be 1.
Test cases:
...
15
votes
40
answers
1k
views
Index and return 3 characters
The challenge is simple: Read 3 letters from a system input¹ and convert them to numbers (A-1, B-2, C-3...Z-26)². Print each of those numbers in the order the ...
15
votes
16
answers
1k
views
How long to carry sort?
Carry sort is an \$O(n)\$ "sorting" algorithm. Here's how it works. The algorithm moves left to right along a list. As it traverses a list it "carries" a single item, the largest ...
26
votes
32
answers
2k
views
Compute minimax of an array
Consider an array x such as [1 5 3 4] and a number n, for example ...
118
votes
397
answers
29k
views
Print a 10 by 10 grid of asterisks
Background
This is a standard textbook example to demonstrate for loops.
This is one of the first programs I learnt when I started learning programming ~10 years ago.
Task
You are to print this exact ...
-6
votes
1
answer
81
views
Do absolutely nothing [closed]
Write a program that has at least one byte that, when run, does absolutely nothing.
I thought this would be done by now, but I can't find any other challenges like this.
How to tell if it does nothing:...
27
votes
54
answers
7k
views
Alphabet to Number and Number to Alphabet
Challenge
In this challenge you have to take a number as input and output the corresponding letter of the alphabet, and vice versa. (1 <=> A, 2 <=> B) etc.
...
20
votes
10
answers
963
views
How far is the Sun?
Introduction
tl;dr
Continuously output the current distance from the Earth to the Sun.
Simplified, the orbit of the Earth around the Sun is an ellipse. So the actual distance between both is ...
55
votes
192
answers
13k
views
Scream very loudly
Write a script that outputs A to stdout infinitely.
There should be no newlines or separators between the characters.
Standard loopholes apply.
This is code-golf. ...
38
votes
14
answers
6k
views
The sum is always 15
Write a program or function that takes an array of non-negative integers as input and outputs a set of vectors/arrays with the elements of the input array in order, split so that each vector sums up ...
23
votes
34
answers
4k
views
Produce a m*n table in HTML
Input :
Two decimal integers m and n that respectively give the number of rows and columns of the table.
...
26
votes
52
answers
4k
views
Is it a pure word?
Introduction
A pure word (or perfect word), as defined by me, is a word where the sum of the position in the alphabet of each letter in the word is perfectly divisible by the total length of the word. ...
52
votes
132
answers
7k
views
The vanilla factorial challenge
Task
Given a non-negative integer \$n\$, evaluate the factorial \$n!\$.
The factorial is defined as follows:
$$
n!=\begin{cases}1 & n=0\\n\times(n-1)!&n>0\end{cases}
$$
Rules
All default I/...
23
votes
9
answers
2k
views
Blinking Lighthouses
Lighthouses on an island have blink codes that let ships know which lighthouse they are seeing. Input should be strings of S and L. Here is a (Python) example of codes for 4 lighthouses as a list of ...
8
votes
51
answers
2k
views
Summing the Digits of a Number
Challenge
Given a non-negative integer, find the sum of its digits.
Rules
Your program must take a non-negative integer as input.
Your program should output the sum of the digits of the input ...
45
votes
88
answers
11k
views
Determine if all decimal digits are unique
Deleted questions on Stack Overflow sometimes make for great golf material.
Write a function that takes a nonnegative integer as input, and returns true if all the digits in the base 10 ...
231
votes
344
answers
61k
views
Is this number a prime?
Believe it or not, we do not yet have a code golf challenge for a simple primality test. While it may not be the most interesting challenge, particularly for "usual" languages, it can be nontrivial in ...
83
votes
203
answers
16k
views
Sum of all integers from 1 to n
I'm honestly surprised that this hasn't been done already. If you can find an existing thread, by all means mark this as a duplicate or let me know.
Input
Your input is in the form of any positive ...
108
votes
319
answers
21k
views
Simple cat program
One of the most common standard tasks (especially when showcasing esoteric programming languages) is to implement a "cat program": read all of STDIN and print it to STDOUT. While this is named after ...
63
votes
153
answers
15k
views
Print 0 to 100 without 1-9 characters
Print 0 to 100 without using characters 1,2,3,4,5,6,7,8,9 in your code.
Seperator of numbers can be comma, whitespace or newline.
Shortest code wins.
18
votes
10
answers
1k
views
Recognize a counting tree
Let a counting tree be a rooted tree in which every node is labeled with the number descendants it has.
We can represent such trees as ragged lists with each node being represented by a list ...
19
votes
28
answers
2k
views
Alternating sums of multidimensional arrays
Given a multidimensional array, find the recursive alternating sum. An alternating sum is simply the sum of an array, where every other item (starting with the second) is negated. For example, the ...
26
votes
47
answers
3k
views
Shortest Code to Find the Smallest Missing Positive Integer
Description
Given an unsorted array of integers, find the smallest positive integer that does not appear in the array. Your task is to write the shortest code possible to solve this problem.
Input
A ...
12
votes
16
answers
2k
views
Longest Valid Parentheses
Given a string of parentheses ( and ), find the length of the longest substring that forms a valid pair of parentheses.
Valid ...
17
votes
24
answers
2k
views
Shortest code to generate all Pythagorean triples up to a given limit
Generate the shortest possible code in any programming language that can generate all Pythagorean triples with all values not exceeding a given integer limit. A Pythagorean triple is a set of three ...
110
votes
83
answers
26k
views
Draw the national flag of Iceland
This year's UEFA Euro 2016 is over and besides a couple of negative headlines there has been a very positive surprise as well – the Iceland national football team. Let's draw their national flag.
...
9
votes
31
answers
2k
views
Counting Letters in a String
Write a program or function that takes in a string and outputs a count of each modern English alphabet letter in the string, case-insensitive.
Input: A string consisting of printable ASCII characters (...
88
votes
116
answers
12k
views
N(e(s(t))) a string
To "function nest" a string, you must:
Treat the first character as a function, and the following characters as the arguments to that function. For example, if the input string was ...
8
votes
14
answers
666
views
Time to shortest permutation
Yesterday, as part of a IQ-style test, I got this interesting question:
The time on a 24-hour clock is 11:43. What is the least number of minutes I should wait before the same digits are on the ...
18
votes
18
answers
1k
views
"Sort" by element duplication
Inspired by one of many bugs I ran into while implementing selection sort in trilangle.
Given a non-empty list of non-negative integers, "sort" the list using the following procedure:
Find ...