Questions tagged [code-golf]
Code-golf is a competition to solve a particular problem in the fewest bytes of source code.
11,748
questions
17
votes
5
answers
778
views
Turn strings into hexagonal spirals!
Goal
Write a function or a program that when given a string, for example "The quick brown fox jumps over the lazy dog", outputs:
...
-2
votes
1
answer
414
views
What is the ABSOLOUTE shortest way to write this code?
So I got kinda bored and was wondering if it was possible to make this code shorter without modifying anything except a file named coin.py...
...
10
votes
3
answers
318
views
Representing a number as an unordered list of smaller numbers
Suppose we want to encode a large integer \$x\$ as a list of words in such a way that the decoder can recover \$x\$ regardless of the order in which the words are received. Using lists of length \$k\$ ...
7
votes
2
answers
269
views
Write the Half-Reactions
A redox reaction is a chemical reaction in which elements transfer electrons. One element loses electrons, while another gains electrons. Oxidation is the process where an element loses electrons, and ...
17
votes
57
answers
3k
views
sum of a range of a sum of a range of n
In this challenge, your input is an integer value. Your task is to find the sum of the range of the sum of the range of n.
Examples:
...
20
votes
23
answers
2k
views
Borders of a Rectangular Matrix
Although it's done a few times as sub-challenge of a larger challenge, and we also have a challenge to remove the borders of a square matrix, I couldn't find a challenge to output the borders of a ...
12
votes
17
answers
2k
views
Compute cumulative means efficiently
Consider a sorted array of positive floating point numbers such as:
input = [0.22, 2.88, 6.35, 7.17, 9.15]
For each integer \$i\$ from 1 up to the last value in ...
0
votes
2
answers
285
views
Implement the Quine-McCluskey algorithm
The Quine-McCluskey algorithm merges disjunctors in a disjunction like
$$
\lnot x_0 \land \lnot x_1 \land \lnot x_2 \land \lnot x_3 \lor\\
x_0 \land \lnot x_1 \land x_2 \land \lnot x_3 \...
3
votes
6
answers
883
views
Tips for golfing in Thunno 2
Thunno 2 is a stack-based golfing language with around 400 commands (achieved through overloads) created by me, The Thonnu.
It seems like a good idea to share some tips for golfing in Thunno 2.
Please,...
41
votes
17
answers
3k
views
Compare Two Fractions With ASCII Art
Challenge
Write a program or function that takes in 4 non-negative integers, A, B, C, and D, that represent two fractions, A/B and C/D, where B and D are non-zero and A <= B and C <= D.
Output ...
22
votes
25
answers
4k
views
Given 4 fence lengths, what's the largest rectangular yard you can make?
Here's a very simple little problem that I don't believe has been asked before.
Challenge
Write a program or a function that takes in four positive integers that represents the lengths of movable but ...
21
votes
13
answers
2k
views
Minkowski's ?(x) for rational x
Here is Minkowski's question mark function:
It is a strictly increasing and continuous function from the reals to themselves that, among other unusual properties, maps rational numbers to dyadic ...
11
votes
6
answers
392
views
Calculate the Smith normal form of an integer matrix
Given an \$m \times n\$ matrix of integers A, there exist a \$m \times m\$ matrix P, an \$m \times n\$ matrix D, and an \$n \times n\$ matrix Q such that:
\$A = P D Q\$.
P and Q are unimodular ...
22
votes
20
answers
2k
views
Calculate Euclidean distance on a torus
Euclidean distance between two lattice points \$(x_1, y_1)\$ and \$(x_2, y_2)\$ on a plane is: \$\sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}\$.
Imagine now a lattice ...
0
votes
1
answer
267
views
Generate all possible equations from a list of numbers [closed]
This is my first codegolf post so let me know if I have missed anything. Thanks :)
Description
You are given a list of numbers with 2 < n <= 6 length i.e. [1, ...
11
votes
9
answers
990
views
Decide Contiguous Binary Tree
Objective
Given an unlabelled binary tree, decide whether it is contiguous in indices.
Indices
This challenge gives one-indexing on binary trees. The exact definition expresses all indices in binary ...
1
vote
2
answers
261
views
Sanitize the Spaces
Given some raw HTML, sanitize it by formatting the spaces, as defined below.
Output rules:
Like many challenges, this one is inspired by XKCD.
First, "untag" all tags with nothing but ...
15
votes
10
answers
3k
views
Elect the Doge of Venice
The Venetian election system was... complicated.
The Great Council came together and put in an urn the ballots of all the councilors who were older than 30. The youngest councilor went to St Mark's ...
16
votes
6
answers
890
views
"Signpost" puzzle from Tatham's collection
Simplified version of "Signpost" puzzle from Simon Tatham's Portable Puzzle Collection.
How the puzzle is created
We pick up randomly positive integer N ...
11
votes
3
answers
386
views
What dice do I need to display my words?
A sequel to What dice do I need to display every integer up to X?
Numbers are great, but let's take it to the next level. I'd like to have a set of dice that can be arranged and rotated to display any ...
16
votes
18
answers
2k
views
Get all URLs on an HTML webpage
This is a rewrite of the (currently closed) "Debunking Stroustrup's debunking of the myth “C++ is for large, complicated, programs only”" challenge.
Challenge
Write the shortest program or ...
16
votes
15
answers
1k
views
Find Index of Rational Number in Calkin-Wilf Sequence
Related
From Wikipedia:
In number theory, the Calkin–Wilf tree is a tree in which the vertices correspond one-to-one to the positive rational numbers. The tree is rooted at the number \$1\$, and any ...
48
votes
9
answers
5k
views
What dice do I need to display every integer up to X?
You may have seen something called a "perpetual dice calendar":
Two 6-sided dice can be rearranged and rotated. One die has the numbers 0, 1, 2, 3, 4, and 5. The second die has the numbers ...
-4
votes
7
answers
1k
views
Straight pen strokes for Prime Numbers
Challenge
You are supposed to output the series I recently designed which goes as follows which are pen stroke counts of ascending prime numbers:
...
24
votes
17
answers
1k
views
Counting fading
Background
Being in a philosophical mood, I remembered one old idea. I think it is suitable for CG.
Duplicates are not found, but it's of course just a kind of totalistic cellular automaton or erosion....
15
votes
13
answers
1k
views
Hankel transform of an integer sequence
A Hankel matrix is a square matrix in which each ascending skew-diagonal from left to right is constant, e.g.:
$$\begin{bmatrix} a & b & c & d \\ b & c & d & e \\ c & d &...
24
votes
16
answers
4k
views
Is it a plausible chess move?
Not a duplicate of the valid move challenge because that asks for specific pieces.
Backstory
The other night I was doing a little trolling with ChatGPT and chess. I was trying to get it to call me out ...
24
votes
5
answers
2k
views
Add two real numbers ... probably
The problem statement here is pretty simple, take two real numbers on the range [0,1) as input and output their sum, with probability 1.
The catch here is that there are a lot of real numbers. There ...
12
votes
15
answers
1k
views
The number of solutions to Hertzsprung's Problem
Hertzprung's Problem (OEIS A002464) is the number of solutions to a variant of the Eight Queens Puzzle, where instead of placing \$n\$ queens, you place \$n\$ rook-king fairy pieces (can attack like ...
10
votes
4
answers
777
views
Identify 22-TET intervals
Objective
Given the distance between two keys in an octave, identify its name.
But there's a caveat. In this challenge, there are 22 keys in an octave, not usual 12.
Here, a porcupine[7] scale will be ...
13
votes
11
answers
2k
views
String Concatenate
You are given a string \$s\$ of characters from a to z. Your task is to count how many unique strings of length \$n\$ you can make by concatenating multiple prefixes of the string \$s\$ together.
...
14
votes
7
answers
1k
views
Gödel encoding - Part II (decoding)
Part I
Previous part was considered encoding of non-empty nested lists with a positive integer.
Reminding the coding procedure \$G(x)\$:
If \$x\$ is a number, \$G(x) = 2^x\$
If \$x\$ is a list \$[n_0,...
11
votes
4
answers
247
views
Shortest Path Around Obstacle
The task here is simple: given a target location on an XY grid, and a rectangle on that grid, find the length of the shortest path from the origin to the target which does not intersect the rectangle.
...
25
votes
38
answers
2k
views
Alternating factorial
The alternating factorial is an alternating sum of decreasing factorials. For example, we could calculate the alternating factorial of 4 as follows:
First, calculate the factorials from 4 down to 1:
...
10
votes
12
answers
1k
views
Calculate Bloons RBE
In Bloons Tower Defense 6, the strength of different bloons can be measured by their Red Bloon Equivalent (RBE), or the number of single pops it takes to completely defeat the bloon.
The RBE for the ...
11
votes
10
answers
2k
views
Parse a CSV file
In this challenge, given a CSV file as a string, you'll return the data contained as a 2d array of strings.
Spec:
The input consists of one or more records, delimited with ...
14
votes
20
answers
2k
views
String Comparison
You are given two strings \$a\$ and \$b\$ consisting of characters from a to z in lowercase. Let \$n\$ be the length of \$a\$. Let \$m\$ be the length of \$b\$. Let \$a'\$ be the string \$a\$ repeated ...
30
votes
18
answers
3k
views
Gödel encoding - Part I
Related but different.
Part II
Taken from the book: Marvin Minsky 1967 – Computation:
Finite and Infinite Machines, chapter 14.
Background
As the Gödel proved, it is possible to encode with a unique ...
11
votes
7
answers
2k
views
Zahlen auf Deutsch! (Numbers in German)
Your task is to write a program that receives a base 10 number from 0 to 99 and outputs the German name of that number with non-ASCII characters replaced with ASCII equivalents.
German number names
<...
3
votes
4
answers
212
views
Determine NBA conference seedings
Heading into the final day of regular season games for the 2023 NBA season, the fifth to ninth seeds in the Western Conference were still very undecided. Four games would determine the seeding:
New ...
17
votes
35
answers
2k
views
The Jaccard Index
The Jaccard index / similarity coefficient, also known as the Tanimoto index / coefficient, is a statistic used for gauging the similarity and diversity of finite sample sets. It was developed by ...
16
votes
7
answers
2k
views
How Super is this Prime?
A super prime is a prime whose index in the list of primes is also a prime:
...
9
votes
4
answers
755
views
Let's go on a hyperjumping trip!
The idea is based on this interactive game. Given a set of eight decimal digits which are not necessarily distinct, your task is to find a hyperjump trip with the specified length.
But what is a ...
21
votes
29
answers
2k
views
Cosine similarity of two vectors
The cosine similarity of two vectors \$A\$ and \$B\$ is defined using their dot product and magnitude as:
\$\frac{A\cdot B}{\|A\|\|B\|}\$
Or in other terms
\$\frac{\sum_{i=1}^nA_iB_i}{\sqrt{\sum_{i=1}^...
12
votes
5
answers
417
views
Enumeration of free polyominoes
A polyomino with \$n\$ cells is a shape consisting of \$n\$ equal squares connected edge to edge.
No free polyomino is the rotation, translation or reflection (or a combination of these ...
17
votes
10
answers
2k
views
Shortest distinguishable slice
For this challenge, a slice of a string is defined as an upper and lower index that can "cut" a piece of a string. All characters from the string in the range ...
27
votes
8
answers
3k
views
Implement grambulation
Consider the following spiral of positive integers:
We now define grambulation as a binary operation \$\lozenge : \mathbb N \times \mathbb N \to \mathbb N\$, using this grid. Some example inputs and ...
8
votes
3
answers
568
views
Doors and guards
Related but noticeably different
You are the leader of the guard in the dungeon of an ancient castle.
There are N doors in the dungeon and ...
14
votes
13
answers
1k
views
Make a Custom Bayer Matrix
A Bayer matrix is a threshold map used for ordered dithering that gives the illusion of having more shades of color than actually present by using a crosshatch-like pattern.
Bayer matrices are square ...
12
votes
7
answers
2k
views
Play Rock-Paper-Scissors 25
RPS 25 is a version of Rock Paper Scissors which has 25 hand symbols instead of just 3. Each symbol defeats 12 symbols, and is defeated by 12 others.
Here's a link to a chart showing which symbols ...