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
18
votes
16answers
776 views

Double trace of a square matrix

Inspired by a question (now closed) at Stack Overflow. Given a square matrix, let its double trace be defined as the sum of the entries from its main diagonal and its anti-diagonal. These are marked ...
-8
votes
0answers
101 views
13
votes
13answers
1k views

Find the smallest solution for this simple equation

Input 4 integers w, x, y, z from the range -999 to 999 inclusive where none of the values is 0. Output 4 integers a, b, c, d so that aw + bx + cy + dz == 0 where none of the values is 0. Restrictions ...
13
votes
15answers
1k views

Print Gobar Primes

Gobar primes (A347476) are numbers which give a prime number when 0's and 1's are interchanged in their binary representation. For example, \$10 = 1010_2\$, and if we flip the bits, we get \$0101_2 = ...
20
votes
9answers
1k views

Self-referential triangle sequence

Output the flattened version of the sequence A297359, which starts like the following: ...
11
votes
8answers
1k views

Find the number of paths in a n×n grid

Information Given a non-negative odd integer (let's call it \$n\$), find the number of all possible paths which covers all squares and get from the start to end on a grid. The grid is of size \$n\$×\$...
13
votes
7answers
3k views

Make S + S + ... + S as Large as Possible!

Let \$S \subset \mathbb N_{\geq0}\$ be a subset of the nonnegative integers, and let $$ S^{(k)} = \underbrace{S + S + \dots + S}_{k\ \textrm{times}} = \{ a_1 + a_2 + \dots + a_k : a_i \in S\}. $$ For ...
14
votes
7answers
1k views

Alphabet Reconstruction

You are a historian studying a long forgotten language. You have just discovered a clay tablet which seems to list all known words in the language, in alphabetical order. Your task is to find this ...
19
votes
3answers
2k views

Crossing a river with weird animals

Background One kind of river-crossing problems involves two kinds of animals. One such problem reads like this: (all wordings, including animal species, are arbitrary) A farmer has to cross a river ...
17
votes
7answers
3k views

Is This Scrabble Board Valid?

Background Scrabble is a word game in which players collaboratively build a board consisting of letter tiles that spell words. Each word must be connected to another word, and players gain points ...
17
votes
31answers
3k views

Pythagorean triples given the hypotenuse

Definition (from Wikipedia) A Pythagorean triple consists of three positive integers a, b, and c, such that a² + b² = c². The typical example of a Pythagorean triple is (3,4,5): 3² + 4² = 9 + 16 = 25 ...
18
votes
15answers
2k views

Is it a prefix normal word?

Prefix normal words arise in the context of binary jumbled pattern matching. A binary word \$w\$ consisting of \$0\$s and \$1\$s is said to be prefix normal * if, among all of its substrings, none ...
23
votes
41answers
2k views

Swap Two Values in a List

Introduction: Although we have a lot of challenges where swapping two items in a list is a subtask, like Single swaps of an array; Swap to Sort an Array; \$n\$ swaps into a nop; etc., we don't have ...
6
votes
2answers
226 views

Every Pong is Abelian

Part of the lean Language of the Month A pong* is a set \$P\$ with an operation \$+\$, that satisfies the following properties: \$ \forall a, b \in P : (b + b) + a = a\\ \forall a, b \in P : a + (b + ...
14
votes
7answers
817 views

Maybe fractal sequence?

Background A fractal sequence (Wikipedia; MathWorld) is an infinite sequence of positive integers meeting the following conditions: Each positive integer appears infinitely many times in the sequence....
12
votes
0answers
332 views

(t)h(e)(i)(r)(m)

Sometimes authors will write (s)he as a stand in for she or he and for some reason they don'...
17
votes
11answers
1k views

How many values of this type?

Background The number of values for a given type is called the cardinality of that type, and that of type T is written as |T|. Haskell and a few other languages ...
9
votes
2answers
466 views

Lean golf: List Game Lv2 (is_in_append, is_in_rev)

The title is an homage of the Natural Number Game, which is a nice interactive tutorial into proving certain properties of natural numbers in Lean. The definitions used in Lv1 will be reused here. I'...
8
votes
2answers
561 views

Follow the border

Inspired by Is this Flow Free puzzle trivial? by @Bubbler. Lengthy chunks of this challenge are borrowed from there. This may be one step of a solution for the linked challenge, depending on chosen ...
10
votes
2answers
646 views

Lean golf: List Game Lv1 (rev_rev)

The title is an homage of the Natural Number Game, which is a nice interactive tutorial into proving certain properties of natural numbers in Lean. Given that the previous two were slightly too ...
6
votes
3answers
349 views

Tips for golfing in Lean

Lean is a theorem prover and programming language. It's also as of writing the Language of the Month! What tips do people have for golfing in Lean? As always, tips should be specific to to Lean (e.g....
14
votes
11answers
884 views

Revert Flow Free puzzle

Inspired by Is this Flow Free puzzle trivial? by @Bubbler. Lengthy chunks of this challenge are borrowed from there. This may be one step of a solution for the linked challenge, depending on chosen ...
10
votes
0answers
293 views

Lean golf: Balanced Bracket Sequence

Ungolfed, ugly, horrible proof to help you make progress on this challenge: https://gist.github.com/huynhtrankhanh/dff7036a45073735305caedc891dedf2 A bracket sequence is a string that consists of the ...
15
votes
1answer
540 views

Lean golf: Pascal vs. Fibonacci

The Pascal's triangle and the Fibonacci sequence have an interesting connection: Source: Math is Fun - Pascal's triangle Your job is to prove this property in Lean theorem prover (Lean 3 + mathlib). ...
40
votes
17answers
5k views

Recursive Stalin Sort

There is a "sorting" algorithm often called Stalin Sort, where instead of sorting an array, you just remove any items that are out of order. In this challenge, you'll implement part of a ...
21
votes
2answers
2k views

∀ a b. a + b = b + a

This question is a part of the lean LotM. A ring is a type of structure that takes the rules of addition and multiplication we are familiar with and abstracts them, so we can reason about them. To do ...
54
votes
45answers
7k views

Increment The Time

Every digital clock contains a small creature that has to advance the time every minute [citation needed]. Due to the popularty of digital clocks and the popularity of catching them in the wild, they ...
17
votes
13answers
2k views

The Area of Rectangles

Getting the area covered by a rectangle is really easy; just multiply its height by its width. However in this challenge we will be getting the area covered by multiple rectangles. This is equally ...
15
votes
44answers
3k views

Implement the hyperfactorial

The objective Given the non-negative integer \$n\$, output the value of the hyperfactorial \$H(n)\$. You don't have to worry about outputs exceeding your language's integer limit. Background The ...
13
votes
15answers
1k views

Create a Formatted Multiplication Table

Your task is to create a good multiplication table for others to use. Given an integer \$n\$, generate a multiplication table that displays products up to \$n×n\$. You should start with \$2×2\$ as 1 ...
14
votes
2answers
496 views

Is this Flow Free puzzle trivial?

Background Flow Free is a series of puzzle games whose objective is to connect all the same-colored pairs of dots on the grid. In this challenge, we consider the original game on a rectangular grid (...
10
votes
5answers
540 views

Khinchin's constant bad estimate [closed]

Inspired by How to write down numbers having an infinity of decimals? Link 🇫🇷 Background From Wikipedia: for almost all real numbers \$x\$, coefficients \$a_i\$ ...
5
votes
6answers
151 views

"Fold" the array [duplicate]

An example will easily explain this to you. Suppose an array [1,2,3,4,5] Step 1 1 2 3 4 5 ----+---- Step 2 ...
10
votes
15answers
2k views

Minimizing flights of stairs climbed

Challenge Given input as a list/array of natural numbers that each represent the floor of a class a student has to go to, sort the numbers in a way such that the MAD (mean average deviation) number of ...
22
votes
19answers
3k views

Find the longest line

Given a string containing only the characters -, |, + and newline determine the longest ...
16
votes
2answers
521 views

Validate Sudoku on a Mobius Strip

@BeastlyGerbil presents, the world's very first MOBIUS SUDOKU: Google Sheets version (The left most square backs onto the right most square, and the middle squares back onto each other. The ends are ...
13
votes
11answers
3k views

Simulate my pill box

I have a small pill box of \$n\$ slots. (Think of it as a linear one like the image below, but not necessarily having 7 slots.) Each slot has at most one pill. Every day, I take one of two actions: ...
9
votes
9answers
425 views

\$n\$ swaps into a nop

Given \$n, m\ge 1\$. There are \$n\$ balls. Do \$m\$ moves, each choose a ball (maybe also the first one), and exchange with the one currently on the first place. At the end, the order of balls should ...
16
votes
15answers
1k views

Drop down the numbers

This was inspired by Apply gravity to this matrix Your challenge is to input a matrix with non-negative one-digit integers and drop down the 1's down 1 column, the 2's down 2 columns, the 3's down 3 ...
7
votes
8answers
303 views

Encrypt this Message [duplicate]

Given a permutation of the alphabet and an input string, encrypt the input string by replacing all characters with the one's you've been given. The capitalization should be kept the same and non-...
14
votes
24answers
1k views

Count occurrences in Pascal's Triangle

Pascal's triangle is a triangular diagram where the values of two numbers added together produce the one below them. This is the start of it: ...
17
votes
17answers
2k views

Longest Uncommon Substring

For the purposes of this challenge a substring \$B\$ of some string \$A\$ is string such that it can be obtained by removing some number of characters (possibly zero) from the front and back of \$A\$. ...
2
votes
6answers
535 views

Multiplication for geometric algebra

The basis vectors for geometric algebra are $$(e_0=1), e_1, e_2,\dots,e_n$$ They all square to 1 (we do not consider vectors which square to -1 or zero) $$e_i \cdot e_i = 1$$ They are associative and ...
15
votes
22answers
1k views

Parentheses Clusters

Write a program that groups a string into parentheses cluster. Each cluster should be balanced. Examples : ...
1
vote
0answers
160 views

Pythagoras' Golfing Grid [closed]

Recently, I created a binary word search that got me working with grids. It was fun, so I wanted to create some more similar content. Meet Pythagoras' Golfing grid: Each of ...
8
votes
7answers
885 views

Best response to the Evolution of Trust? - Part 1

Backstory I was fiddling around with the Evolution of Trust and thought of this.Warning: I recommend you go check it out first before attempting this challenge. Summary for people who haven’t played A ...
23
votes
28answers
2k views

Triple countdown sequence

Let's start with the natural numbers ...
17
votes
4answers
892 views

Find the sliced sheet of paper

Context : Suppose you have a sheet of paper measuring 8 x 10. You want to cut it exactly in half while maintaining its rectangular shape. You can do this in two ...
21
votes
24answers
3k views

Apply gravity to this matrix

This was inspired by this question. Given an \$m\times n\$ matrix of \$0\$'s and \$1\$'s, apply "gravity" to it. This means to drop down all the \$1\$'s as if they were affected by gravity. ...
7
votes
2answers
313 views

Setting up Al's R session

Al wanted to use my computer to do some simple calculations, so I lent him the use of my R terminal for a bit. He complained though, saying it wasn't set up how he liked it. I said "No problem Al,...

1
2 3 4 5
213