Questions tagged [code-golf]
Code-golf is a competition to solve a particular problem in the fewest bytes of source code.
12,076
questions
1
vote
0
answers
23
views
Count squares in my pi approximation
One way to approximate π is the following: Start by drawing a 2x2 square with a quarter-circle in it. Then, the area of the quarter-circle is π.
We can approximate this area by filling it with ...
3
votes
3
answers
42
views
Smallest prime q such that concatenation (p+q)"q is a prime
Let p, q, and c := (p + q)"q (where " denotes concatenation) be positive integers such that p and c are primes and q is the smallest prime such that c is prime.
Such a prime triple (p, q, (p ...
8
votes
1
answer
738
views
-6
votes
4
answers
162
views
Branchless median of 5
Write code to compute the median of 5 positive 32 bit integers. Your code must be branchless.
A branch, jump or transfer is an instruction in a computer program
that can cause a computer to begin ...
9
votes
12
answers
1k
views
Convert base-10 to base-0.1
Base-0.1 mean e.g. 123.45 means in decimal \$1\times 0.1^2+2\times 0.1^1+3\times 0.1^0+4\times 0.1^{-1}+5\times 0.1^{-2}\$.
Input and output should match RegEx <...
10
votes
6
answers
1k
views
ASCII 2D landscape
You are given an input string, called path, consisting of u, d and s characters (i.e. ...
9
votes
14
answers
2k
views
Function with memories of its past life
Task
I need a function that accept string and return a function f1.
That f1 function should also accept string and return a <...
-3
votes
0
answers
153
views
I cant figure out how to make this python function shorter but I am convinced it is possible. Does anyone have any idea how I can achieve this? [closed]
Every time I dig too deep into this function I lose my trail of thought but I am almost certain it is possible to simplify.
The code rotates points in 3 dimensions in my small project where I made a ...
15
votes
10
answers
1k
views
Sylvester primes
Sylvester's sequence can be defined recursively S(n) = S(n-1)*(S(n-1) + 1) for n >= 1 starting S(0) = 1.
Since S(n) and S(n) + 1 have no common divisors, it follows that S(n) has at least one more ...
16
votes
12
answers
3k
views
Is it a date format of YYMMDD, MMDDYY, and/or DDMMYY?
The date format varies between countries; the arrangement of date, month and year can be categorized into these three:
Year-month-date
Month-date-year
Date-month-year
In Gregorian calendar it's year ...
12
votes
9
answers
1k
views
How many ways can you make change?
The "third type of Euler Transform" takes an integer sequence that gives the number of objects of a given weight and outputs a sequences that gives the number of multisets of objects that ...
6
votes
2
answers
286
views
Snake a string through a simplex
An n-simplex is a generalization of 'triangleness' in any dimension (specifically, it is the simplest shape requiring n dimensions). Starting with 0 dimensions, the named simplexes are: point, line ...
9
votes
3
answers
217
views
Convert to Chomsky normal form
Let a context-free language be specified by its production rules and start symbol, e.g.
S -> AbB | C
C -> b | c
B -> AA | AC
A -> a | ε
Here and in the ...
9
votes
9
answers
723
views
Encode a VarInt
Similar question to my previous one, but in reverse. This time I want you to encode an integer into a VarInt.
Background:
I'm not going to assume everyone's seen my previous question, so here is a ...
14
votes
18
answers
2k
views
Stuck on Sokoban
In the classic game of Sokoban the player's character solves puzzles by pushing boxes around a warehouse onto designated targets. Take, for example, this ASCIIfied game level:
...
19
votes
12
answers
2k
views
Parse Minecraft's VarInt
If you look at the reverse-engineered Minecraft Java Edition server protocol, there is an interesting integer encoding method that is used quite often called VarInt.
It works like this:
You have an ...
29
votes
21
answers
3k
views
Completely introduce your friends
I have a friend with a peculiar habit (more, a long-running joke) of introducing everyone present at an event in this fashion:
Alice, Bob. Bob, Tom. Tom, Bob. Bob...
When there are more than a few ...
-1
votes
1
answer
58
views
Function to Convert string to Hexadecimal [duplicate]
Assignment:
Create a function that can convert a string to its encoded hexadecimal value.
In your answer, also show how to run this function (to validate answers).
Your function must return OR print ...
7
votes
8
answers
544
views
Curry a C function pointer [closed]
Objective
Given a type signature of a C function pointer represented as a string, output the (fully) curried version of it, also as a string.
I/O format
It is assumed that:
There is at least one ...
14
votes
13
answers
1k
views
Calculate the sum of numbers in a rectangle
Aim
Consider this infinite array of numbers:
...
11
votes
4
answers
647
views
Miracle Miracle Octad Generator Generator
The Miracle Octad Generator (MOG) introduced by R. T. Curtis in 1976 is a central tool in the study of sporadic groups, error-correcting codes, Steiner systems, and sphere-packings.
The MOG is ...
18
votes
5
answers
1k
views
Ways to paint a backbone on a tree
Say I have some unlabelled tree graph:
I'll define a "backbone" as a path on a graph that can't be extended - both its ends are at terminal vertices. There are three ways to overlay a ...
16
votes
19
answers
2k
views
Erase the loops
A nice simple one!
You are given a finite sequence. Find the earliest pair of equal elements, if any (where "earliest" refers to the position of the second element of the pair), and ...
14
votes
5
answers
604
views
Sum-of-four-squares grid
Output a grid of characters visualizing the decomposition of a number into a sum of four perfect squares.
Challenge
Given a nonnegative integer \$0 \le n \le 2^{30}\$, output a \$2^k \times 2^k\$ ...
15
votes
15
answers
1k
views
Sums of X*Y chunks of the nonnegative integers
Consider the infinite table of the nonnegative integers with width 12:
...
17
votes
18
answers
2k
views
Shift right by half a trit
Shift right by half a trit
This is inspired by Shift right by half a bit, but it's a little different.
Motivation
I was wondering if there is a function f that maps ...
6
votes
7
answers
503
views
Generalized Super-Luhn
Background
Luhn's Algorithm is a simple check digit formula used to validate a variety of identification numbers. In standard Luhn, each second digit from the end is doubled, and then the digits of ...
11
votes
14
answers
803
views
Is there mutable aliasing in this list of variable references?
Rust's borrow checker enforces the requirement of "mutability XOR aliasing": a piece of data can either have multiple immutable references or a single mutable reference. This challenge ...
21
votes
28
answers
3k
views
Make a GCSE student's error work
A GCSE computer science student recently made this hilarious blunder on their exam and posted it online. Now, you must make it work:
...
13
votes
15
answers
1k
views
Do something you can't do in Brainfuck [closed]
Goal
Do some action (like delete a file, connect to a server, …) that you can not do in standard Brainfuck.
What
As you know, all calculations you can do in almost any programming language you can ...
28
votes
48
answers
2k
views
Cubic Concatenation
Challenge
Given three non-negative integers \$a, b\$ and \$c\$, decide if the sum of their cubes is equal to the concatenation of those numbers, aka:
$$
a^{3}+b^{3}+c^{3} = a^\frown b ^\frown c
$$
...
13
votes
8
answers
889
views
Lexicographically earliest permutation of the initial segment of nonnegative integers subject to divisibility constraints
The challenge is simple: Reorder the first integers {0, 1, 2, ..., n} into an ordered list so that the following three conditions are met:
If k is the last element in the list, then all of its prime ...
8
votes
7
answers
1k
views
Is this a valid Irish word?
In Irish, most consonants are divided into broad (velarized) and slender (palatalized) variants, and the orthography marks them with neighboring vowels, which are similarly divided. This gives rise to ...
6
votes
1
answer
248
views
Counterexample to Shapiro inequality
Input: A positive integer n such that n is even and greater than 12 or n is odd and greater than 23.
Output: A list of non-negative integers that violates Shapiro inequality.
More precisely, Let s be ...
9
votes
3
answers
801
views
Automatic Pikmin Count
Inspired by the feature in Pikmin 4.
In Pikmin 4, you can have up to eight types of pikmin in an onion and up to three types of pikmin outside the onion. Each area recommends three types of pikmin to ...
14
votes
16
answers
569
views
Rabinowitz-Wagon \$\pi\$ formula
In 1995, Stanley Rabinowitz and Stan Wagon found an interesting algorithm to generate the digits of \$\pi\$ one by one without storing the previous results. The algorithm is called the spigot ...
12
votes
19
answers
2k
views
Three up to 800 & sum to 2048
(Flexibly) output 3 random integers such that:
Each number is 1 to 800 inclusive
Their sum is ...
18
votes
39
answers
2k
views
Find the newest element
Given a finite sequence, each of its elements has a first occurrence. Your task is to output the symbol whose first occurrence is last in the sequence. If the elements represent posts and reposts of ...
10
votes
10
answers
752
views
Factoriadic Fraction Addition
Objective
Given two rational numbers represented in fractional factoriadic as defined below, add them, and output the result in fractional factoriadic.
Fractional factoriadic
Fractional factoriadic is ...
14
votes
16
answers
2k
views
Reduce a string up to idempotency [closed]
Objective
Given a string consisting of printable ASCII characters (!0x21 ― ~0x7E), treat it as an element in the free idempotent ...
3
votes
1
answer
194
views
How comparison-heavy is this list? [closed]
Given a list of strings in any convenient format display the amount of comparisons between individual characters that a merge sort algorithm would perform.
The merge-sort algorithm is a divide & ...
11
votes
5
answers
496
views
Double dequer sort
In this challenge Bubbler describes sorting a list by inserting its elements left to right into a double ended queue or "deque".
Take a number from the front of the array, and push it into ...
10
votes
4
answers
749
views
GolfScript many-items rotation
There isn't a builtin in golfscript which lets you rotate a bigger portion of the stack than the three top-most elements.
It should take one number n as argument and rotate the nth value from the top, ...
17
votes
4
answers
1k
views
Substring of Fizzbuzz
Decide if a string is substring of Fizzbuzz without separation 12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz....
Notice that it doesn't necessary cover whole tokens, ...
23
votes
20
answers
2k
views
Simulate Text Cursor
You are given a string [A-Za-z|]+ that contains exactly one occurrence of | denoting the current position of the cursor and a ...
7
votes
9
answers
1k
views
Braille-based Base64
Make a one-to-one map from the base64 character table(0-9a-zA-Z+/) to Braille(U+2800-U+283F).
These characters are fixed map for compatibility. For other characters(aka. A-Z and +) you can map any ...
2
votes
0
answers
240
views
Shortest way to cause a seg fault at 0x9c?
In honour of CrowdStrike's exception at 0x9c, what's the shortest way (character-wise) to cause a segfault by dereferencing address 0x9c?
The best I can come up with is:
...
7
votes
6
answers
627
views
Compute Dickman
Input
A floating point number \$x\$ between 1 and 8 inclusive.
Output
The Dickman function of \$x\$.
The Dickman–de Bruijn function \$\rho(u)\$ is a continuous function that satisfies the delay ...
4
votes
18
answers
430
views
Check if two lists are permutations of each other [duplicate]
Challenge
Given two lists of equal length, find if one of them is a permutation of the other. Output truthy or falsy values, or 1 or 0.
Test case Examples
...
13
votes
17
answers
1k
views
Reorder for smallest largest prefix sum
Given a multiset of integers, order them such that the largest absolute prefix sum $$\max_i \left| \sum_{k<i} a_k \right|$$ is smallest. Output any solution if multiple exist.
You can assume input ...