Questions tagged [code-golf]
Code-golf is a competition to solve a particular problem in the fewest bytes of source code.
11,079
questions
40
votes
13
answers
4k
views
Recreate Minecraft's lighting
Minecraft has a fairly unique lighting system. Each block's light value is either one less than the brightest one surrounding it, or it is a light source itself. Your task is to write a method that ...
11
votes
14
answers
871
views
Arrange a Haft-Sin table
It's Nowruz and you want to set up a Haft-Sin table by words. This means finding seven words that start with letter s.
The Challenge
Write a program which its input is a list of words separated by ...
13
votes
14
answers
2k
views
Sandbox overflow
Your task is to output a square sandbox of size n+2 with / characters in the corners and | ...
3
votes
2
answers
290
views
Calculate the highest possible period oscillator in Conway's Game of Life
Input: a whole positive number greater than 4
Output: A whole positive number that describes the highest possible period an oscillator could have inside a square space of size (input)x(input) in a &...
19
votes
12
answers
901
views
Convert between graph representations
What?
Let's say I have this graph:
1
\
\
2 3
\ /
\ /
4
I can represent it in 2 ways:
A list of connected vertices. ...
73
votes
53
answers
11k
views
Draw the Ukrainian Flag
As you will probably know, there is a war going on in Ukraine. I noticed that it seems nobody has posted a Ukrainian flag challenge yet, so I thought I'd do it myself in support of Ukraine.
The ...
10
votes
6
answers
413
views
The Missing Match
You are given a string of unknown length that contains a varied amount of (, {, [ and their ...
18
votes
15
answers
1k
views
Long period primes
A long period prime is a prime number \$p\$ such that decimal expansion of \$1/p\$ has period of length \$(p-1)\$. Your task is to output this number sequence. For purposes of this challenge we will ...
22
votes
26
answers
2k
views
Randomly Rounding
Input a decimal number and round it to an integer, randomly rounding up or down with a probability based on its fractional part, so the expected value of the output equals to the input value.
If ...
15
votes
13
answers
1k
views
Recursively rotate a ragged list
Given a ragged list of positive integers return a full cycle of recursive rotations starting with the unchanged input and ending with the state immediately before revisiting the initial state.
...
22
votes
32
answers
5k
views
Negative of an ASCII photo
Task
Given an \$m\times n\$ binary ascii "photo", return the negative of the photo
Example:
...
4
votes
19
answers
551
views
Double bit rotation to the right [closed]
Given a positive integer as input, output that integer, but with its bits rotated two times to the right. Also, think of the number as a donut of bits, eg. ...
12
votes
3
answers
273
views
Line Islands in a Word Search
My third word search related challenge in a row. :)
Challenge:
Brief explanation of what a word search is:
In a word search you'll be given a grid of letters and a list of words. The idea is to cross ...
20
votes
14
answers
1k
views
Sort numbers in a ragged list
Given a ragged list, e.g.
[[4, 7], [5, 3, [], [6, [2]]]]
Your challenge is to sort only the numbers in it. For example, with the above, the result would be
...
11
votes
25
answers
1k
views
Circular chained compound
Task
Given a positive integer n, output the joined compound of circles
The circles are of square of size n+2 but removing the ...
7
votes
13
answers
1k
views
Complex permutation
Given an array of integers as input(including negative integers), find all (can include duplicates) permutations that do not have the same number next to each other, then print them all out.
If no ...
20
votes
26
answers
2k
views
Zip uneven lists
In this challenge you will take two lists as input and you will zip them. The zip can be defined with this recursive expression:
\$
\mathrm{zip}(a,b) = \begin{cases}
\left[\,\,\right] & a = \left[...
3
votes
9
answers
1k
views
int, float or neither?
Given a string, determine if it is an int, a float or neither.
Examples
123 - int
62727.0033 - float
644c.33 - neither
Your code should output a different value depending on which it is. For example ...
11
votes
7
answers
437
views
Sort musical pitches
Given an unordered list of musical pitches, write the shortest program/function (scored in bytes) to sort the list from lowest pitch to highest.
Pitches will be given in scientific pitch notation, ...
22
votes
17
answers
942
views
Pairs of integers ordered by their exponentiation
Output the infinite list of pairs of integers (a, b), where both \$ a > 1 \$ and \$ b > 1 \$, ordered by the value of \$ a^b \$. When there are multiple pairs ...
25
votes
13
answers
853
views
Resolve references in a chat discussion
Sometimes in chat, if someone says something you agree with, you'll send a message with an ^, which points at the message above:
...
28
votes
29
answers
2k
views
Repeat List Until Longer
The Challenge
The challenge is simple: given an input list a and another list b, repeat a ...
15
votes
9
answers
871
views
Count the Liberties
Background
This challenge is about the game of Go. Go is a game played on a board with a square grid N x N. You don't have to know how to play the game to do this ...
2
votes
5
answers
142
views
Generate this string table [duplicate]
For the given string s, generate generate a table like below.
...
14
votes
16
answers
1k
views
Building spikes
Inspired by this building (United States Air Force Academy Cadet Chapel, Colorado)
Your task is simple, given a positive integer n, output the spiked building to ...
6
votes
2
answers
355
views
Reveal all clues of Black Box
Black Box is a board game, Your task is to reveal all clues.
What is black box
Black box is a board game with hidden atoms, Your task is given input, All atoms, reveal all clues.
I/O
Input
The atoms ...
3
votes
5
answers
320
views
The Nineteenth Bakery [duplicate]
Yes, this is very similar to this question. However, that concerns infinite linked lists, and you do not need to handle out of bounds indexes in that question.
You are in a strange city, looking for ...
16
votes
10
answers
1k
views
Gray code on N symbols
A Gray code is a permutation on binary numbers where incrementing or decrementing a number only ever causes a single bit to change. For example, a Gray code for the binary numbers up to 7 (so, with 3 ...
18
votes
9
answers
2k
views
Is it irrational?
Your task is to make a program that decides if a real number between 0 and 1 is irrational or not. As stated, this is obviously impossible, so instead we will use the following definition:
...
12
votes
7
answers
654
views
S̶t̶r̶i̶k̶e̶t̶h̶r̶o̶u̶g̶h̶ the Word Search List
Introduction:
Every workday I read the newspaper in the morning while eating breakfast, and also do some of the puzzles at the back. When I do the word-search puzzles, I (almost) always go over the ...
14
votes
2
answers
504
views
Find the magic numbers to divide a number without division
An integer \$x\in[0,2^{32}-1]\$ divided by an integer \$d\in{[1,2^{31}]}\$ will produce an integral quotient \$q\$ and a remainder \$r\$, so that \$x=d\times q+r\$.
Any \$q\$, in fact, can be ...
15
votes
3
answers
337
views
What was my Wordle play?
Task
Given a winning Wordle play results (clues), the target ("secret") word and a list of words, output a possible sequence of words leading to such results.
The words in the list will be ...
17
votes
12
answers
2k
views
Random Point from a 2D Donut Distribution
A donut distribution (for lack of a better term) is a random distribution of points in a 2-dimensional plane, forming a donut-like shape. The distribution is defined by two parameters: the radius <...
25
votes
27
answers
2k
views
Fill in the next numbers
Given a list of integers, output a list where the \$ i \$th element of the list equals the first number in the input list which is \$ \ge i \$, until there are no such numbers.
For example, if the ...
14
votes
14
answers
989
views
Lexicographically Smallest Decreasing Integer Partition
For a given number n, output an strictly decreasing array whose sum is n and its lexicographical order is the smallest for any ...
21
votes
1
answer
529
views
Can you draw this in one stroke?
Related | Related
Given an ASCII art with |, _, and , check if you can draw the art in one ...
20
votes
20
answers
2k
views
Can the Tune be Played?
Can the Tune be Played?
Explanation
A broken musical keyboard has keys labelled with positive integers. It is broken in two ways:
It takes a long time to process key presses: after pressing the key ...
5
votes
1
answer
316
views
Convert 256-wrap brainfuck into 257-wrap brainfuck
Usually each cell in brainfuck has 256 states, which is not enough to tell whether input is EOF or a character (usually \xFF). To fix it we can add one more state ...
8
votes
7
answers
282
views
Ascii Fibonacci spiral
Let's continue the fibonacci based challenges stream, here's the next one:
Task
Draw a Fibonacci spiral ascii-art of n segments where starting from the first term:
each nth segment has a length of ...
4
votes
0
answers
167
views
Calculate the probability of getting this hand in n tries [closed]
Imagine there is a card game where the primary objective of the game is to get 4 of one card in your hand.
Game mechanics
Each player starts of with 10 cards in their hand. The remaining 32 cards (...
10
votes
1
answer
290
views
Page Selector⠀⠀⠀⠀⠀
I'm making a page selector for my super cool terminal-based blog program. In order for it to fit correctly into the design, it has to have some constraints.
The page selector can only display a ...
19
votes
21
answers
2k
views
Fibonacci triangle
Given an integer n, output the following ASCII art to n rows:
1+1=2
1+2=3
2+3=5
3+5=8
5+8=13
Essentially, the first row is ...
6
votes
5
answers
558
views
Crate art stacking
Your task is, given a array of numbers, that is strictly increasing, output the crate stack. The input is an array of positive integers which represents which layer has how many crates there are. So ...
31
votes
19
answers
2k
views
Laggy text editor
Your challenge is to write a program that constantly prompts for input, and when input is given, output that five seconds* later. However, your program must continue prompting for input during that ...
20
votes
21
answers
3k
views
Gambling with an Alien
You met a 4-th dimensional being who challenged you to a game of dice. The rules are simple: each player rolls 3 6-sided dice and takes the sum of each combination of 2 dice. The player with the ...
2
votes
3
answers
267
views
Appease the Picky Eater
Your friend Jack is a picky eater. He only likes certain foods, and he only likes to eat a certain amount of them each day. Despite this, Jack has very strict calorie and macronutrient goals that he ...
31
votes
45
answers
3k
views
Output a random unary string
Your task is simple: output the letter x a random number of times. Every possible length of xs must have a non-zero probability ...
5
votes
4
answers
572
views
Which Western Zodiac sign is today?
Task:
With the minimum amount of bytes, determine today's Zodiac sign using the programming language of your choice.
Rules:
This is code golf, the answer with the fewest characters wins. The ...
29
votes
16
answers
2k
views
How-many-bonacci-like is this sequence?
Inspired by @emanresu A's Is it a fibonacci-like sequence? Make sure to upvote that challenge as well!
We say a sequence is Fibonacci-like, if, starting from the third term (\$1\$-indexed), each term ...
13
votes
3
answers
504
views
Solve an alien wordle
You've arrived on an alien planet. The writing system, the culture and the language there are completely foreign to any language you know. But somehow they are all playing wordle already.
So as a ...