Questions tagged [code-golf]

Code-golf is a competition to solve a particular problem in the fewest bytes of source code.

Filter by
Sorted by
Tagged with
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: ...
pan's user avatar
  • 481
-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... ...
Eric's user avatar
  • 107
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\$ ...
Karl's user avatar
  • 621
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 ...
noodle man's user avatar
  • 4,026
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: ...
Breadleaf's user avatar
  • 467
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 ...
Kevin Cruijssen's user avatar
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 ...
Simd's user avatar
  • 1,493
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 \...
Roman Czyborra's user avatar
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,...
The Thonnu's user avatar
  • 17.5k
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 ...
blaketyro's user avatar
  • 769
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 ...
blaketyro's user avatar
  • 769
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 ...
Parcly Taxel's user avatar
  • 3,707
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 ...
Daniel Schepler's user avatar
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 ...
anatolyg's user avatar
  • 13.1k
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, ...
Kyle Sharp's user avatar
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 ...
Dannyu NDos's user avatar
  • 5,143
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 ...
Infigon's user avatar
  • 505
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 ...
KeizerHarm's user avatar
  • 1,171
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 ...
lesobrod's user avatar
  • 3,231
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 ...
swinn's user avatar
  • 575
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 ...
bigyihsuan's user avatar
  • 8,248
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 ...
97.100.97.109's user avatar
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 ...
swinn's user avatar
  • 575
-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: ...
Aitzaz Imtiaz's user avatar
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....
lesobrod's user avatar
  • 3,231
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 &...
alephalpha's user avatar
  • 45.6k
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 ...
lyxal's user avatar
  • 31.4k
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 ...
Wheat Wizard's user avatar
  • 95.5k
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 ...
bigyihsuan's user avatar
  • 8,248
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 ...
Dannyu NDos's user avatar
  • 5,143
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. ...
Huỳnh Trần Khanh's user avatar
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,...
lesobrod's user avatar
  • 3,231
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. ...
97.100.97.109's user avatar
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: ...
chunes's user avatar
  • 20.9k
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 ...
evanstar3's user avatar
  • 358
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 ...
Borkbot's user avatar
  • 111
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 ...
Huỳnh Trần Khanh's user avatar
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 ...
lesobrod's user avatar
  • 3,231
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 <...
Creative Name's user avatar
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 ...
Parcly Taxel's user avatar
  • 3,707
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 ...
solid.py's user avatar
  • 1,597
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: ...
Lecdi's user avatar
  • 1,155
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 ...
polfosol ఠ_ఠ's user avatar
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}^...
emirps's user avatar
  • 1,747
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 ...
math scat's user avatar
  • 7,889
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 ...
noodle man's user avatar
  • 4,026
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 ...
caird coinheringaahin g's user avatar
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 ...
lesobrod's user avatar
  • 3,231
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 ...
Yousername's user avatar
  • 3,430
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 ...
97.100.97.109's user avatar