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
21
votes
38answers
971 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 ...
30
votes
26answers
4k views

Dutch Burgerservicenummer (BSN) eleven-test

Introduction: A Dutch BSN (BurgerServiceNummer) is valid when it complies to the following rules: It only contains digits. The length should be either 8 or 9 in length. When the digits are indexed as ...
15
votes
14answers
567 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 ...
54
votes
43answers
6k 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 ...
40
votes
111answers
11k views

Print 0 to 100 without 1-9 characters

Print 0 to 100 without using characters 1,2,3,4,5,6,7,8,9 in your code. Seperator of numbers can be comma, whitespace or newline. Shortest code wins.
1
vote
6answers
520 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 ...
9
votes
0answers
213 views

Lean golf: Balanced Bracket Sequence

A bracket sequence is a string that consists of the characters ( and ). There are two definitions of a balanced bracket sequence....
16
votes
6answers
1k views

Define the finite field GF(9)

\$GF(9)\$ or \$GF(3^2)\$ is the smallest finite field whose order isn't a prime or a power of two. Finite fields of prime order aren't particurlarly interesting and there are already challenges for \$...
28
votes
25answers
3k views

Calculate A190810

Your task is pretty simple, calculate the n-th element of A190810. Elements of A190810 are calculated according to these rules: The first element is 1 The sequence is increasing If ...
20
votes
19answers
1k views

Multiple-Key Sorting

Given a list of indices and zero or more lists of integers, output the lists of integers, sorted in ascending order, with the key priority from the first input. Example Let the keys input be ...
111
votes
375answers
27k views

Print a 10 by 10 grid of asterisks

Background This is a standard textbook example to demonstrate for loops. This is one of the first programs I learnt when I started learning programming ~10 years ago. Task You are to print this exact ...
12
votes
11answers
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 ...
6
votes
2answers
166 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 + ...
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 ...
23
votes
6answers
618 views

Determine How many Wheels There Are

Non-math explanation This is an explanation that is meant to be approachable regardless of your background. It does unfortunately involve some math, but should be understandable to most people with a ...
14
votes
7answers
758 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....
32
votes
41answers
6k views

Character Cross

I was expecting to post something more complex as my first puzzle in PCG, but a particular, uh... homework question on Stack Overflow inspired me to post this. They want to: print the following ...
38
votes
41answers
4k views

The Rien Number

The Champernowne constant is a number that is constructed by concatenating the first n numbers, with n tending to infinity. It ...
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 ...
83
votes
103answers
14k views

Hello World 0.0!

source: Dilbert, September 8, 1992 I'm hoping to add a new twist on the classic "Hello World!" program. Code a program that outputs Hello World! without: String/...
39
votes
16answers
4k 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 ...
20
votes
35answers
3k views

Does this month start on a Monday?

June 2020 is a month in which June 1st corresponds to Monday, June 2nd corresponds to Tuesday, ... June 7th corresponds to Sunday. For reference, here's the cal of ...
9
votes
2answers
456 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'...
21
votes
13answers
3k views

Codegolf the permanent

The challenge is to write codegolf for the permanent of a matrix. The permanent of an \$n\times n\$ matrix \$A = a_{i,j}\$) is defined as $$\text{perm}(A) = \sum_{\sigma \in S_n} \prod^n_{i=1} a_{i,\...
47
votes
52answers
9k views

We had a question once which only failed on Sundays

Inspired by We had a unit test once which only failed on Sundays, write a program or function that does nothing but throw an error when it is Sunday, and exit gracefully on any other day. Rules: No ...
479
votes
868answers
161k views

"Hello, World!"

So... uh... this is a bit embarrassing. But we don't have a plain "Hello, World!" challenge yet (despite having 35 variants tagged with hello-world, and counting). While this is not the most ...
12
votes
0answers
260 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'...
21
votes
28answers
3k views

Find me vowels near you

Write a function that takes in a string and for each character, returns the distance to the nearest vowel in the string. If the character is a vowel itself, return 0. Vowels are : ...
14
votes
6answers
949 views

Find the shortest route on an ASCII road

You want to find the length shortest path between two points, on an 2d ASCII "map". The roads are made up of + characters, and the two endpoints are represented by <...
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 ...
32
votes
16answers
4k views

Calculate 500 digits of pi

Write a program to calculate the first 500 digits of pi, meeting the rules below: It must be less than 500 characters in length. It cannot include "pi", "math.pi" or similar pi constants, nor may it ...
26
votes
6answers
7k views

ASCII Pizza (no pineapple)

I like pizza! Task Given the radius of a pizza and a list of ingredients, create the corresponding ascii pizza! Example size 4 pizza with mozzarella cheese, olives and ham: ...
33
votes
2answers
2k views

Addition on Elliptic Curves

Disclaimer: This does not do any justice on the rich topic of elliptic curves. It is simplified a lot. As elliptic curves recently got a lot of media attention in the context of encryption, I wanted ...
19
votes
31answers
3k views

Find the perfect square!

Your task is to turn a square root like this: √12 into a form like this: 2√3 For our purpose, we only need to output the left ...
15
votes
1answer
525 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). ...
28
votes
21answers
5k views

Egg, sausage, bacon and spam (lovely spam!)

You are given four integers: \$e,s,b\in\{0,1\}\$ and \$S\in \{0,1,2,4\}\$, where \$e,s,b,S\$ stand for egg, sausage, bacon and spam respectively. Your task is to figure out whether the corresponding ...
8
votes
2answers
547 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 ...
47
votes
28answers
4k views

Turing-Complete Language Interpreter

A challenge I thought that would be very cool is to make an interpreter for a Turing-complete language of your choosing. The rules are simple: You may use any language to create this interpreter ...
14
votes
11answers
865 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 ...
31
votes
6answers
1k views

Implement true string addition

Many languages allow strings to be "added" with +. However this is really concatenation, a true addition would follow the group axioms: It is closed (...
12
votes
2answers
702 views

How long will it take Santa to deliver his presents?

I posted this challenge a while ago, which concerns how many elves Santa needs to deliver presents. Due to population increase, Santa is a little more pressed for time this year. Although in the past ...
22
votes
50answers
4k views

Print the digital root

This is different from My Word can beat up your Word as it is less complex and only requires you to calculate it, and not compare them. To find the digital root, take all of the digits of a number, ...
31
votes
59answers
8k views

Convert a string of binary characters to the ASCII equivalents

Take a string of binary characters separated by a space, and convert it to an ASCII string. For example... ...
34
votes
26answers
2k views

No one, please!

I absolutely hate the digit 1. So, I need your help to convert numbers to their "proper forms". Numbers in proper form never have two ...
59
votes
55answers
10k views

Determine your language's version

Your challenge is to write a polyglot that works in different versions of your language. When run, it will always output the language version. Rules Your program should work in at least two versions ...
39
votes
103answers
6k views

The vanilla factorial challenge

Task Given a non-negative integer \$n\$, evaluate the factorial \$n!\$. The factorial is defined as follows: $$ n!=\begin{cases}1 & n=0\\n\times(n-1)!&n>0\end{cases} $$ Rules All default I/...
21
votes
8answers
1k views

Rolling Quine Dice

Create a program that outputs itself. However, if the source code is repeated n times (meaning to concatenate a copy of the source code to the end n-1 times), then there should be 1/n probability ...
18
votes
14answers
1k views

Unfold a list using a function

Task Haskell's and Scala's standard libraries have an unfold function that builds a list from an initial state s and a function <...
19
votes
28answers
2k views

Pendulum Encoding

Given a string as an input (which can be any acceptable/convenient format in your language), implement pendulum encoding. The test cases are split into individual items (which aren't quoted) for a ...
103
votes
111answers
17k views

Code Golf Christmas Edition: How to print out a Christmas tree of height N

Given a number N, how can I print out a Christmas tree of height N using the least number of code characters? ...

1
2 3 4 5
213