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
12
votes
12answers
105 views

How many bystanders will help?

Flavortext The Bystander Effect is a phenomenon where individuals are less likely to help a victim if other people are present. The idea is that as there are more people around, the individual burden ...
-1
votes
0answers
108 views

The Great Battle Conundrum- Who are Alive? [closed]

Maximillian is the chief commander of the Great Greek Army and he is leading his forces into a crucial war with Spain. If all the enemy soldiers stand in a straight line incrementally marked starting ...
10
votes
4answers
271 views

Shrinking Triangles

Write an infinite list of triangle codes, defined as code where the i-th line has i bytes for all lines, such that the i-th code generates (i-1)th code and has 1 more line than the (i-1)th code. The ...
16
votes
15answers
1k views

How hyperperfect am I?

A \$k\$-hyperperfect number is a natural number \$n \ge 1\$ such that $$n = 1 + k(\sigma(n) − n − 1)$$ where \$\sigma(n)\$ is the sum of the divisors of \$n\$. Note that \$\sigma(n) - n\$ is the ...
20
votes
34answers
1k views

Output a Latin Square

An \$n\times n\$ Latin Square is a grid containing exactly \$n\$ distinct values where the values in each row and column are distinct. For example, $$\begin{matrix} A & B & C \\ C & A &...
17
votes
18answers
997 views

Make it prime with the smallest suffix

Given a positive integer as input, output the smallest positive integer such that appending its digits (in base 10) to the end of the input number will form a prime number. Examples ...
8
votes
3answers
148 views

Interpret control characters like a terminal

Given a ASCII string containing control characters, compute what it should look like when printed to a terminal. Imagining the behaviour of a cursor, this is how to treat each character in the input: ...
6
votes
5answers
222 views

Matching homologous groups

Homologous series, any of numerous groups of chemical compounds in each of which the difference between successive members is a simple structural unit. As an example, Alkane is a homologous group ...
14
votes
20answers
895 views

Remove first encountered elements from a second list

Inspired by this question on Stackoverflow. The task Let's have two lists/arrays of integers: L1 and L2 of equal length. You ...
16
votes
6answers
675 views

Minimal number of banknotes to pay a bill

Suppose denominations of banknotes follow the infinity Hyperinflation sequence: \$ $1, $2, $5, $10, $20, $50, $100, $200, $500, $1000, $2000, $5000, \cdots \$. How many banknotes are required, at ...
8
votes
3answers
193 views

Rejecting invalid IPv4 addresses

I want to avoid responding to illegal or spoofed addresses. One type that's easy to detect is an address that should not exist on a packet received by my router: a special-use address. Challenge Given ...
13
votes
10answers
603 views
+500

The longest consecutive traversal possible (number of edges)

A truck fleet dispatcher is trying to determine which routes are still accessible after heavy rains flood certain highways. During their trips, trucks must follow linear, ordered paths between ...
7
votes
4answers
161 views

Basic Typescript Types

Typescript is a typed superset of Javascript. For this challenge, we'll consider the following classic basic types: string ...
22
votes
19answers
1k views

Calculate Home Primes

The Home Prime of an integer \$n\$ is the value obtained by repeatedly factoring and concatenating \$n\$'s prime factors (in ascending order, including repeats) until reaching a fixed point (a prime). ...
10
votes
1answer
123 views

Golf your finger-strokes in Flow Free: Warps

Background Flow Free is a series of puzzle games whose objective is to connect all the same-colored pairs of dots on the grid. Flow Free: Warps introduces the "warping" mechanic: a line ...
4
votes
3answers
120 views

Repeating terms of a given sentence [closed]

Program Write a program to display repeating terms of a given sentence. Explanation The program must output the repeating terms of two or more letters from the given input (input is a sentence ...
17
votes
17answers
1k views

The meeker number sequence

The Meeker numbers are a 7 digit number in form of \$abcdefg\$, where \$a×b=10c+d\$ and \$d×e=10f+g\$. As an example \$6742612\$ is a meeker number, here \$6×7=10×4+2\$ and \$2×6=10×1+2\$, so it is a ...
17
votes
1answer
281 views

Determine Circles

Giving n(any amount) of points (x,y). What's the minimum amount of circles required to cross every point given? Task Your ...
11
votes
11answers
1k views

Variable length Fibonacci word

Challenge For any two non-empty strings A and B, we define the following sequence : F(0) = A F(1) = B F(n) = F(n-1) + F(n-2) Where ...
16
votes
15answers
2k views

Number of coins needed to make change

Relatable scenario: I'm going to the store to buy a single item, but only have a $100k bill. As a result, I need exactly $99,979 in change, and in the fewest coins/bills possible because I'm quite ...
1
vote
4answers
188 views

A problem of rarity [closed]

Given a positive input \$n > 0\$, output the amout of two types based on their rarity. The two types are called \$A\$ and \$B\$, we know the followings: \$n\$ is a limited input and the maximum is ...
16
votes
19answers
1k views

Multiplicative Persistence #2

We had a challenge on Multiplicative Persistence here. As a recap, to get a multiplicative persistence of a number, do these steps: Multiply all the digits of a number (in base \$10\$) Repeat Step 1 ...
8
votes
1answer
222 views

The number of tilings of a grid

Setup: A block is any rectangular array of squares, specified by its dimensions \$(w,h)\$. A grid is any finite ordered list of blocks. For example, \$\lambda = ((3,2),(3,1),(1,2))\$ defines a grid. ...
21
votes
8answers
2k views

Implement Ash's float division

Ash has a bit of an interesting float division algorithm. It's designed to never return NaN, and things like signed zero and infinity need to be handled. How it ...
1
vote
0answers
94 views

Identify the liars [closed]

There are \$a\$ honest man(always tell the truth), \$b\$ dishonest man(always tell lie), and \$c\$ random man(tell random Y/N). How many times at least should you ask one of them a yes/no question ...
6
votes
10answers
857 views

Random pair of primes

Given a positive input \$n\$, output a random pair of primes whose difference is \$n\$. It's fine if there's another prime between them. Every pair should possibly appear and the program should have ...
11
votes
5answers
485 views

Outsmart the boffins at New Scientist (string rotation)

The brainiacs at New Scientist tweeted this morning that "The word STABLE has the neat property that you can cycle the first letter to the end to make a new word: TABLES. And you can do it again ...
23
votes
25answers
2k views

Overwrite a string on a tape

Take the string abcdfghjk. To overwrite it, starting from f, with the string wxyz, you would ...
19
votes
15answers
3k views

Complex Fibonacci numbers

The Binet formula is a closed form expression for the \$n\$'th Fibonacci number: $$F_n = \frac {\phi^n - (1-\phi)^n} {\sqrt 5}$$ where \$\phi = \frac {1 + \sqrt 5} 2\$ is the golden ratio. This ...
12
votes
4answers
464 views

The Game of Cat And Mice | With Matrices

Challenge This coding challenge is to figure out how many rounds the cat can live. In a \$4\times4\$ matrix, there are a number of mice and exactly 1 cat. Example: $$ \begin{array} {|r|r|}\hline 🐭 &...
13
votes
9answers
609 views

Truncate continued fractions

Related: Cleaning up decimal numbers Background A continued fraction is a way to represent a real number as a sequence of integers in the following sense: $$ x = a_0 + \cfrac{1}{a_1 + \cfrac{1}{a_2 + \...
23
votes
19answers
1k views

Possible periods of input

Background: Take this input as an example: 1 1 2 1 1 2 1 3 1 3 If you look only at the first few digits, between 1 1 2 and <...
22
votes
19answers
1k views

Quoted rational numbers

Quote notation is a way of expressing rational numbers based on the concept of \$p\$-adic numbers, written in the form \$x'y\$. The quote indicates that the number to it's left (\$x\$) is "...
19
votes
3answers
2k views

Calculate longest Stack Exchange streak

Introduction This challenge is inspired by the Meta Stack Exchange question The longest consecutive days streak; it turns out that the URL ...
21
votes
19answers
2k views

Code the Levine sequence

Introduction Note that I learned it from a Numberphile Video, where Neil Sloane explains it better. I recommend you to watch his Video. But for a quick Introduction: The Levine Sequence is made from ...
16
votes
5answers
661 views

Dominate a zero-sum game

Consider a zero-sum game with 2 contestants. Each round, each contestant chooses, independently of each other, one of \$n \ge 2\$ different choices. Depending on the two chosen choices, one player is ...
9
votes
16answers
3k views

Double it? Then halve it! But multiply it? Then divide it!

Idea from the doubler challenge, this may be more difficult. The program should output a positive integer or decimal point value higher than 0. If I duplicate the source code, the output should halve ...
5
votes
5answers
109 views

Sample space of n consecutive coin flips [duplicate]

Taking a positive integer n as input, print the sample space of n consecutive coin flips. The coin is fair, with two sides ...
14
votes
8answers
1k views

How often will this test fail?

Preamble There was a unit test in our codebase which was shuffling a string of length \$52\$ formed from the set of letters \$[A-Z]+[A-Z]\$ and then using the first \$20\$ characters of that shuffled ...
5
votes
2answers
116 views

Shortest Google Sheets formula to detect if cell contains an image

Step one In a new google sheet insert an image into cell A1 with Insert > Image > Image In Cell Step two Create a formula to return TRUE (if the cell contains an image) and FALSE (if the image ...
-3
votes
5answers
152 views

Pretty printing JSON

I want format a JSON string into human-readable form. A string like this: ...
8
votes
4answers
579 views

Sr. 4Der says make this shape! (Episode 1)

Dialogue by a 3Der. I was hired by Sr. 4Der to learn the 4D universe and I was willing to get to know more about it. But since I am a 3Der, I wouldn't see him, nor would I know how the real shapes ...
6
votes
13answers
625 views

Equalizing fractions

When I was in grade 3, we were taught how to solve a very simple math problem. It was equaling the denominators of two or more fractions. Let's take two proper fractions:- $$ \frac{1}{2},\frac{2}{3} $$...
21
votes
3answers
386 views

Sticky polyhexes

Background A polyhex of size \$n\$ is a contiguous shape made from joining \$n\$ unit regular hexagons side-by-side. As an example, the following image (from Wikipedia) contains all 7 distinct ...
17
votes
20answers
2k views

Fibonacci Encoding

Fibonacci coding is a universal code, which can encode positive integers of any size in an unambiguous stream of bits. To encode an integer \$ n \$: Find the largest Fibonacci number less than or ...
17
votes
18answers
1k views

Check B-powersmoothness

from Wikipedia, a number is called B-powersmooth if all prime powers \$p^v\$ that divide the number satisfy \$p^v \leq B\$. B-powersmoothness is important, for example, for Pollard's p-1 factorization ...
7
votes
5answers
335 views

Average impact of multiple conversion errors

Imagine four people stand in a line. The first looks at a thermometer, and tells the temperature to the person on their right. This continues down the line, until the last person writes the ...
7
votes
5answers
218 views

(Σ*)² ⟲ Σ* (Round-Trip a String Pair)

We're back! And this time with an objective scoring criteria! This is a challenge I thought of recently which I think is not hard but allows for a wide range of possible solutions. Seems like a ...
14
votes
2answers
457 views

What's my TIO uniqueness?

Try it online! currently has 680 listed languages, all of which are searchable. For most of those languages, when searching for it, you don't have to enter the entire string. For example, entering <...
16
votes
21answers
1k views

Trim elements from the ends of a list

The aim is to trim elements in from the ends of a list, depending on if they are in a second list of "falsey" results. For example, if the first list is ...

1
2 3 4 5
206