Questions tagged [code-golf]
Code-golf is a competition to solve a particular problem in the fewest bytes of source code.
10,267
questions
5
votes
4answers
24 views
Remove first encountered elements from a second list
Inspired by this question on Stackoverflow.
The task
Let's have two lists/arrays of integers: L1 and L2 of equal length. You ...
4
votes
0answers
39 views
Minimal number of banknotes to pay a bill
Suppose denominations of banknotes follow the infinity Hyperinflation sequence: \$ $1, $2, $5, $10, $20, $50, $100, $200, $500, $1000, $2000, $5000, \cdots \$. How many banknotes are required, at ...
5
votes
1answer
77 views
Rejecting invalid IPv4 addresses
I want to avoid responding to illegal or spoofed addresses. One type that's easy to detect is an address that should not exist on a packet received by my router: a special-use address.
Challenge
Given ...
12
votes
8answers
324 views
The longest consecutive traversal possible (number of edges)
A truck fleet dispatcher is trying to determine which routes are still accessible after heavy rains flood certain highways. During their trips, trucks must follow linear, ordered paths between ...
6
votes
3answers
104 views
Basic Typescript Types
Typescript is a typed superset of Javascript. For this challenge, we'll consider the following classic basic types:
string
...
16
votes
18answers
443 views
Calculate Home Primes
The Home Prime of an integer \$n\$ is the value obtained by repeatedly factoring and concatenating \$n\$'s prime factors (in ascending order, including repeats) until reaching a fixed point (a prime). ...
9
votes
1answer
90 views
Golf your finger-strokes in Flow Free: Warps
Background
Flow Free is a series of puzzle games whose objective is to connect all the same-colored pairs of dots on the grid. Flow Free: Warps introduces the "warping" mechanic: a line ...
4
votes
3answers
115 views
Repeating terms of a given sentence [closed]
Program
Write a program to display repeating terms of a given sentence.
Explanation
The program must output the repeating terms of two or more letters from the given input (input is a sentence ...
15
votes
15answers
840 views
The meeker number sequence
The Meeker numbers are a 7 digit number in form of \$abcdefg\$, where \$a×b=10c+d\$ and \$d×e=10f+g\$. As an example \$6742612\$ is a meeker number, here \$6×7=10×4+2\$ and \$2×6=10×1+2\$, so it is a ...
17
votes
1answer
268 views
Determine Circles
Giving n(any amount) of points (x,y). What's the minimum amount of circles required to cross every point given?
Task
Your ...
11
votes
11answers
1k views
Variable length Fibonacci word
Challenge
For any two non-empty strings A and B, we define the following sequence :
F(0) = A
F(1) = B
F(n) = F(n-1) + F(n-2)
Where ...
14
votes
15answers
2k views
Number of coins needed to make change
Relatable scenario: I'm going to the store to buy a single item, but only have a $100k bill. As a result, I need exactly $99,979 in change, and in the fewest coins/bills possible because I'm quite ...
1
vote
4answers
179 views
A problem of rarity [closed]
Given a positive input \$n > 0\$, output the amout of two types based on their rarity. The two types are called \$A\$ and \$B\$, we know the followings:
\$n\$ is a limited input and the maximum is ...
16
votes
19answers
1k views
Multiplicative Persistence #2
We had a challenge on Multiplicative Persistence here.
As a recap, to get a multiplicative persistence of a number, do these steps:
Multiply all the digits of a number (in base \$10\$)
Repeat Step 1 ...
8
votes
1answer
216 views
The number of tilings of a grid
Setup:
A block is any rectangular array of squares, specified by its dimensions \$(w,h)\$. A grid is any finite ordered list of blocks. For example, \$\lambda = ((3,2),(3,1),(1,2))\$ defines a grid.
...
20
votes
7answers
2k views
Implement Ash's float division
Ash has a bit of an interesting float division algorithm. It's designed to never return NaN, and things like signed zero and infinity need to be handled.
How it ...
1
vote
0answers
92 views
Identify the liars [closed]
There are \$a\$ honest man(always tell the truth), \$b\$ dishonest man(always tell lie), and \$c\$ random man(tell random Y/N). How many times at least should you ask one of them a yes/no question ...
6
votes
10answers
851 views
Random pair of primes
Given a positive input \$n\$, output a random pair of primes whose difference is \$n\$. It's fine if there's another prime between them.
Every pair should possibly appear and the program should have ...
11
votes
5answers
480 views
Outsmart the boffins at New Scientist (string rotation)
The brainiacs at New Scientist tweeted this morning that "The word STABLE has the neat property that you can cycle the first letter to the end to make a new word: TABLES. And you can do it again ...
23
votes
25answers
2k views
Overwrite a string on a tape
Take the string abcdfghjk. To overwrite it, starting from f, with the string wxyz, you would ...
19
votes
15answers
3k views
Complex Fibonacci numbers
The Binet formula is a closed form expression for the \$n\$'th Fibonacci number:
$$F_n = \frac {\phi^n - (1-\phi)^n} {\sqrt 5}$$
where \$\phi = \frac {1 + \sqrt 5} 2\$ is the golden ratio. This ...
12
votes
4answers
458 views
The Game of Cat And Mice | With Matrices
Challenge
This coding challenge is to figure out how many rounds the cat can live.
In a \$4\times4\$ matrix, there are a number of mice and exactly 1 cat.
Example:
$$
\begin{array}
{|r|r|}\hline
🐭 &...
13
votes
9answers
608 views
Truncate continued fractions
Related: Cleaning up decimal numbers
Background
A continued fraction is a way to represent a real number as a sequence of integers in the following sense:
$$
x = a_0 + \cfrac{1}{a_1 + \cfrac{1}{a_2 + \...
23
votes
19answers
1k views
Possible periods of input
Background:
Take this input as an example:
1 1 2 1 1 2 1 3 1 3
If you look only at the first few digits, between 1 1 2 and <...
22
votes
19answers
1k views
Quoted rational numbers
Quote notation is a way of expressing rational numbers based on the concept of \$p\$-adic numbers, written in the form \$x'y\$.
The quote indicates that the number to it's left (\$x\$) is "...
19
votes
3answers
2k views
Calculate longest Stack Exchange streak
Introduction
This challenge is inspired by the Meta Stack Exchange question The longest consecutive days streak; it turns out that the URL ...
21
votes
19answers
2k views
Code the Levine sequence
Introduction
Note that I learned it from a Numberphile Video, where Neil Sloane explains it better. I recommend you to watch his Video. But for a quick Introduction:
The Levine Sequence is made from ...
16
votes
5answers
656 views
Dominate a zero-sum game
Consider a zero-sum game with 2 contestants. Each round, each contestant chooses, independently of each other, one of \$n \ge 2\$ different choices. Depending on the two chosen choices, one player is ...
9
votes
16answers
3k views
Double it? Then halve it! But multiply it? Then divide it!
Idea from the doubler challenge, this may be more difficult.
The program should output a positive integer or decimal point value higher than 0.
If I duplicate the source code, the output should halve ...
5
votes
5answers
108 views
Sample space of n consecutive coin flips [duplicate]
Taking a positive integer n as input, print the sample space of n consecutive coin flips. The coin is fair, with two sides ...
13
votes
8answers
1k views
How often will this test fail?
Preamble
There was a unit test in our codebase which was shuffling a string of length \$52\$ formed from the set of letters \$[A-Z]+[A-Z]\$ and then using the first \$20\$ characters of that shuffled ...
5
votes
2answers
116 views
Shortest Google Sheets formula to detect if cell contains an image
Step one
In a new google sheet insert an image into cell A1 with
Insert > Image > Image In Cell
Step two
Create a formula to return TRUE (if the cell contains an image) and FALSE (if the image ...
-3
votes
5answers
151 views
8
votes
4answers
570 views
Sr. 4Der says make this shape! (Episode 1)
Dialogue by a 3Der.
I was hired by Sr. 4Der to learn the 4D universe and I was willing to get to know more about it. But since I am a 3Der, I wouldn't see him, nor would I know how the real shapes ...
6
votes
13answers
620 views
Equalizing fractions
When I was in grade 3, we were taught how to solve a very simple math problem. It was equaling the denominators of two or more fractions.
Let's take two proper fractions:-
$$
\frac{1}{2},\frac{2}{3}
$$...
20
votes
3answers
372 views
Sticky polyhexes
Background
A polyhex of size \$n\$ is a contiguous shape made from joining \$n\$ unit regular hexagons side-by-side. As an example, the following image (from Wikipedia) contains all 7 distinct ...
17
votes
20answers
2k views
Fibonacci Encoding
Fibonacci coding is a universal code, which can encode positive integers of any size in an unambiguous stream of bits.
To encode an integer \$ n \$:
Find the largest Fibonacci number less than or ...
17
votes
18answers
1k views
Check B-powersmoothness
from Wikipedia, a number is called B-powersmooth if all prime powers \$p^v\$ that divide the number satisfy \$p^v \leq B\$. B-powersmoothness is important, for example, for Pollard's p-1 factorization ...
7
votes
5answers
331 views
Average impact of multiple conversion errors
Imagine four people stand in a line. The first looks at a thermometer, and tells the temperature to the person on their right. This continues down the line, until the last person writes the ...
7
votes
5answers
218 views
(Σ*)² ⟲ Σ* (Round-Trip a String Pair)
We're back! And this time with an objective scoring criteria!
This is a challenge I thought of recently which I think is not hard but allows for a wide range of possible solutions. Seems like a ...
13
votes
2answers
451 views
What's my TIO uniqueness?
Try it online! currently has 680 listed languages, all of which are searchable. For most of those languages, when searching for it, you don't have to enter the entire string. For example, entering <...
16
votes
21answers
1k views
Trim elements from the ends of a list
The aim is to trim elements in from the ends of a list, depending on if they are in a second list of "falsey" results.
For example, if the first list is ...
24
votes
27answers
2k views
Un-pipe an Elixir expression
Elixir is a programming language with a feature called the pipe operator, |>, similar to the pipe in Bash and other languages. It passes the result of an ...
26
votes
3answers
855 views
Placing circles along a square spiral
In this code golf challenge, you'll be computing the placement of circles of areas \$\pi, 2\pi, 3\pi, \dots\$ when greedily placed along integer points in a square spiral in such a way that no two ...
12
votes
10answers
2k views
Calculate the probability of getting to the target first (exactly)
Consider the following probability puzzle.
We start with a string of bits all set to 0. At each step we choose a bit uniformly and independently at random and flip it. The value your code has to ...
8
votes
1answer
206 views
I'm Jelly of Python (Robbers)
What does "jelly" mean in the title?. Cop's thread
Robbers, you are to choose an uncracked Cop answer, and attempt to find a Jelly program which is:
shorter than the Cop's answer
solves the ...
11
votes
3answers
922 views
I'm Jelly of Python (Cops)
What does "jelly" mean in the title?. Robber's thread
Jelly has an "Evaluate the argument as Python 3" atom, ŒV which takes a string as an ...
25
votes
27answers
1k views
Decode Polybus Square/Tap Code/Prison Code
The code has a lot of names, but is very simple:
1 2 3 4 5
1 A B C D E
2 F G H I J
3 L M N O P
4 Q R S T U
5 V W X Y Z
A letter is coded by its coordinates, ...
12
votes
5answers
566 views
Which electrical outlets can I use?
In North America, most electrical outlets/receptacles follow standards set by NEMA. For this challenge, you'll be given a few properties of a device you need to plug in, and your program (or function) ...
16
votes
2answers
228 views
Decompress an integer, Jelly style
Jelly has compressed string literals, using the “...» delimiters. The way these work is by interpreting the ... as a base-250 ...