Questions tagged [code-golf]
Code-golf is a competition to solve a particular problem in the fewest bytes of source code.
11,460
questions
6
votes
4
answers
239
views
It's a dog! (Maybe)
The city defines a dog as any living entity with four legs and a tail. So raccoons, bears, mountain lions, mice, these are all just different sizes of dog.
Given an ASCII-art image of an animal, ...
15
votes
8
answers
1k
views
Exactly two neighbors
Given as input a list of positive integers, your task is to determine if every integer present has exactly two neighbors in the list. The neighbors of a integer \$x\$ are the distinct integers that ...
12
votes
15
answers
1k
views
Distance to the average of the next two prime numbers
Suppose we have a sequence \$P\$. Every element \$P_n\$ represents the distance between the \$n^{th}\$ prime number and the average of the next two prime numbers.
For example, \$P_1\$ would be the ...
17
votes
9
answers
1k
views
Reverse the polyglot, change the language
Task
Write a polyglot in two languages that when run, outputs the name of the language it is run in. Additionally, this program must also output the other language's name if the source code is ...
7
votes
14
answers
949
views
Sum numbers which are strings
Your function must accept 2 strings which are correct numbers. It needs to sum them up (without rounding and floating point errors) and return the result (which is also a correct number) without any ...
22
votes
28
answers
2k
views
Sum of Consecutive Squares
Your task
Given a integer input, \$ n \$ (such that \$ n > 1 \$), decide whether it can be written as the sum of (at least 2) consecutive square numbers.
Test cases
Truthy:
...
19
votes
16
answers
3k
views
Don't repeat yourself
In this challenge you will be tasked with implementing a sequence of natural numbers such that:
Each number appears a natural number of times
No two numbers appear the same number of times
No two ...
22
votes
30
answers
2k
views
Smallest Bit Rotation
For a given positive integer, try to find out the smallest possible rotation resulted by rotating it 0 or more bits.
For example, when the given number is 177, whose binary representation is \$...
16
votes
9
answers
2k
views
Yakko's New World Order
Given a list of countries in alphabetical order, put them in the same order as they are given in the song "Yakko's World".
Specifically, you are given the following list of countries* ...
12
votes
8
answers
2k
views
Help me sell old donuts
You work at a bakery, and every day you make pastries. You make 100 of each of several different types. However customers are less predictable. Some days they order all of one kind of pastry and you ...
-1
votes
0
answers
89
views
Gimme some sums [closed]
Gimme some sums
In this challenge we are going to have a little fun with lists. Your output should contain
exactly the following (down to the 14 dashes). Leading and trailing characters are permitted, ...
1
vote
4
answers
196
views
Get the date of the Nth day of week after the Xth day of week in a given year and month
Introduction
In the United States, national elections are normally held on the first Tuesday after the first Monday in November.
Challenge
Write a program that takes a Year as integer, and a pair of (...
27
votes
30
answers
2k
views
Output a random value from the last 16 values outputted
Output the numbers 1-16 (or any other set of 16 distinct items). Then, repeatedly, output a random value chosen uniformly from the last 16 items outputted.
After the same item is printed 16 times in a ...
13
votes
16
answers
2k
views
Create a bingo card generator
Bingo
Bingo is a numbers game where players match randomly drawn numbers to the numbers on their cards. Each bingo card is a square grid with 25 spaces, and the columns of the grid are labeled with ...
8
votes
3
answers
275
views
Knook to Mate... With Portals!
Alternatively: Now you're checking with portals!
Inspired this challenge
I am somewhat of a self-declared Chess Anarchist. That means that when I play chess, en passant is forced (else the brick gets ...
14
votes
12
answers
2k
views
Salacious Bacon Tripod
In a certain chatroom, we like making acronym jokes about the build tool we use called sbt. While it usually stands for "Scala Build Tool", we aim to come up with all sorts of meanings, such ...
6
votes
5
answers
1k
views
Best Iterated Prisoner's Dilemma Move
Introduction
In the prisoner's dilemma, two partners in crime are being interrogated, and have the choice to either betray their partner or stay silent.
If both prisoners betray each other, they both ...
23
votes
5
answers
1k
views
Pushing boxes away
You are on a plane filled with boxes, and you get a sequence of movements, say "up right right down left":
You move up, pushing all boxes in front of you away:
You move right, right and ...
15
votes
8
answers
7k
views
Implement a 2fuck interpreter
2fuck is a simple 2D variation on brainfuck. Like traditional brainfuck, there is an 1D memory tape that extends 30000 bytes to the right from its beginning, consisting of values between 0 and 255.
...
9
votes
7
answers
710
views
Parse basic arithmetic into an AST
Introduction
An abstract syntax tree (AST) is a tree of tokens that shows their connection to each other and syntactical meaning. They are commonly used in compilers and static analysis tools, as ...
15
votes
14
answers
1k
views
Painting with Line Filler
Given a matrix of positive integers, output whether it's possible to generate it by starting with an empty1 matrix of the same dimensions and repeatedly filling a whole row or a whole column with the ...
8
votes
17
answers
2k
views
Capture the Flag... with a twist
Inspired by a challenge from the OUCC 2022 Seniors competition.
Background
Two teams are playing "capture the flag". They take turns invading each other's base and capturing their opposing ...
8
votes
7
answers
512
views
Print the notes of an increasing octave-repeating scale
Challenge
To quote Wikipedia:
An octave-repeating scale can be represented as a circular arrangement
of pitch classes, ordered by increasing (or decreasing) pitch class. For instance, the increasing ...
9
votes
13
answers
1k
views
Simplify a Cycle
Alternatively: That one challenge I forgot I had in the sandbox and is about stuff from Discrete Mathematics I learned like 5-6 months ago and kinda don't remember
Given a path of vertices that form a ...
22
votes
32
answers
2k
views
Shortest digid-app GetPercentageRounds function
Context
So the Dutch "Ministerie van Binnenlandse Zaken en Koninkrijksrelaties" (NL) - "Ministry of the Interior and Kingdom Relations" (EN) recently released the source code of ...
10
votes
9
answers
751
views
Find the length of the longest substring with all different characters in O(n) time
Let's solve the same task as in this challenge but faster!
Input: a non-empty string containing letters a-z
Output: the length of a longest (contiguous) substring in which all letters are different
...
14
votes
8
answers
1k
views
Change the subject
Sometimes I feel like the conversation is going down the wrong path and take it upon myself to steer it in another direction. And this conversation is going wrong real fast, so I need to change the ...
16
votes
12
answers
1k
views
Is this a shift matrix?
A Shift matrix is a binary matrix with one superdiagonal or subdiagonal formed by only ones, everything else is a zero.
A superdiagonal/subdiagonal is a diagonal parallel to the main diagonal, which ...
23
votes
14
answers
3k
views
Knight to fork!
In chess, fork means to target two pieces with just one piece, and one of the best pieces for forking is the knight.
In this challenge, you will be given three coordinates in ...
13
votes
8
answers
1k
views
Toggle some bits and get an actual square
Inspired by the title of the Toggle some bits and get a square challenge.
In that challenge you output how many bits should be toggled, in order for the base-10 representation of the binary to become ...
8
votes
4
answers
390
views
Number-only switch
Write a function that takes an ASCII-printable-only (all charcodes ∈ [32..126]) string input x and provides an ASCII-printable-only output string ...
24
votes
21
answers
2k
views
Implement a bag without replacement
Intro
The Tetris Guidelines specify what RNG is needed for the piece selection to be called a Tetris game, called the Random Generator.
Yes, that's the actual name ("Random Generator").
In ...
5
votes
2
answers
697
views
Tips for shortening this type-safe addition in JavaScript
Consider:
c=(a,b)=>(+a||0)+(+b||0)
Yes, it is not good to solve it like that. But I want to shorten it even more. If a is a ...
9
votes
9
answers
1k
views
Fewest dice rolling as dice rolling
Related
Now you have some dice which you may roll several times. You need a uniform random integer between 1 and \$n\$, inclusive. Show a method that rolls the fewest times and behaves as an \$n\$-...
15
votes
13
answers
661
views
Form a subset that is a continuous range
Given a list of lists of positive integers, output a subset of them so their union forms a continuous non-empty range with no numbers missing. For example, consider this input:
...
14
votes
21
answers
2k
views
Find the first run of numbers summing to n
Given as input a positive nonzero integer n >= 10 and a sequence of digits 0-9 (which may be taken as a string or a list), ...
0
votes
1
answer
113
views
Tips for golfing in Wren
Wren is a small, fast, class-based concurrent scripting language. Does anyone have any tips for this language?
Please make sure to post 1 tip per answer, and don't post answers like "remove ...
15
votes
17
answers
3k
views
Guess the song title
I have this problem where I listen to so much music that I can never remember what songs are called. But even if I remember the lyrics, I won't always know the song's name. But lucky for me, there's a ...
12
votes
17
answers
2k
views
Remove duplicates from my academic transcript
Consider a list of subject, grade pairs. E.g.
...
14
votes
14
answers
1k
views
n-digit primes given the first m digits
I just discovered this site and this is my first question, I hope I'm doing it correctly.
The challenge is the following, you must write a code that prints all the prime numbers of \$n\$ digits. Since ...
16
votes
15
answers
1k
views
Division between two strings
Inverse function of this challenge
To (properly) multiply an string by an integer, you split the string into characters, repeat each character a number of times equal to the integer, and then stick ...
17
votes
3
answers
523
views
Intersection area of two rotated rectangles
Given two rectangles, which are possibly not in the orthogonal direction, find the area of their intersection.
Input
You may take the rectangles as input in one of the following ways:
The ...
8
votes
9
answers
1k
views
Wordle games without repeating letters
Given a word list, find all sets of five words of five letters each, such that the words of each set have 25 distinct letters in total.
This challenge was inspired by this video by Matt Parker.
...
12
votes
10
answers
2k
views
Round to nicer numbers
The standard way to round numbers is to choose the nearest whole value, if the initial value is exactly halfway between two values, i.e. there is a tie, then you choose the larger one.
However where I ...
0
votes
0
answers
130
views
Tips for golfing in Zig
Not sure if I've seen this one before :/
Any tips for golfing in Zig? Please post 1 tip per answer and don't post obvious tips like "remove whitespace".
Github link
12
votes
10
answers
862
views
Maximum of outer product of integer vectors (in linear time)
Introduction
Our goal is to efficiently find the maximum of a large amount of (redundant) data.
We define the outer product of vectors \$A\$ and \$B\$ as a matrix containing the products of all ...
12
votes
6
answers
837
views
ASCII-art polygons to GeoJSON coordinates
We're going to turn ascii art versions of polygons into their equivalent GeoJSON.
The ASCII shape language
The input ASCII language only has 3 possible characters:
...
5
votes
2
answers
285
views
Don't output the file 2
Last time you were required to write program that output a file, such that no byte is same as original file. The request meant to let everyone carry part of information, but it ends up that everyone ...
19
votes
16
answers
2k
views
Is it true? Ask Pip!
Title is an homage to this classic challenge
In Pip, the Scalar data type encompasses strings and numbers. A Scalar value is truthy in most cases. It is falsey only if:
It is the empty string ...
13
votes
21
answers
892
views
Find the Prime Signature
The Prime Signature of a number is the list of the exponents of the prime factors of a number, sorted in descending order (exponents of 0 are ignored). Inspired by ...