Questions tagged [code-golf]
Code-golf is a competition to solve a particular problem in the fewest bytes of source code.
12,370 questions
2
votes
2
answers
216
views
Speak in Binary
Background
This video goes into ways you can humanize binary, including creating a more compact writing notation using . and | ...
4
votes
2
answers
177
views
Identify Redundant Infix Condition
Say there are three multisets A, B, C. An infix condition has form
<ON|FACING|NEAR> <a multiset>
where
ON requires ...
11
votes
4
answers
542
views
Minimum width to connect both sides
Given two sorted lists of same length \$\{a_i\}\$ and \$\{b_i\}\$, find the smallest \$w\$ such that it's possible to connect each pair of points \$(0,a_i)\$ and \$(w,b_i)\$ with paths such that
x-...
0
votes
1
answer
112
views
Golf a number bigger than all other answers [duplicate]
You have to code in python, and the number generated by your code must be bigger than all other current submissions. You need to make your code as small as possible, it has to terminate but you can ...
-1
votes
1
answer
135
views
Generare a toothbrush diagram [closed]
Given a String of opening brackets and the letter G, such that it makes a applicative tree of the leaf node G and ( representing ...
8
votes
4
answers
605
views
Find a primitive polynomial
Objective
Given a prime number \$p\$ and an integer \$n \geq 2\$, find a degree-\$n\$ primitive polynomial modulo \$p\$.
Mathematical explanation
When we perform "modular arithmetic" over ...
14
votes
10
answers
1k
views
Polynomial Basis Conversion
The most common way to represent a polynomial is writing it as a linear combination of monomials, i.e., powers of the variable. For example, the polynomial \$p(x) = x^3 + 2x^2 + x + 1\$ is a linear ...
18
votes
18
answers
2k
views
Order a list by the difference of its elements
Task
Given an unsorted list of integers, order it in such a way that the absolute difference of every two adjacent elements will always be equal to 1:
\$|dx| = 1\$
There will be guaranteed one or more ...
11
votes
7
answers
612
views
Sticks expectation
This was originally a question from interviewstreet.com over a decade ago
You are given an array (or whatever - your choice of input) of positive integers \$y_1,\ldots,y_n\$ that represents \$n\$ line ...
18
votes
8
answers
1k
views
English to Dutch number order conversion
Introduction:
When saying numbers out loud, the order is slightly different in Dutch than in English. For a number like \$1234\$:
in English you'd say: one thousand, two hundred and thirty-four
in ...
11
votes
17
answers
924
views
Identify Redundant Regex
A redundant regular expression (regex) is defined here as a regex which has some amount of characters which can be removed while not affecting its functionality -- i.e. it matches the exact same set ...
8
votes
8
answers
971
views
Validate DNA Sequences with Telomeres
This is inspired by this homework assignment.
Telomeres are regions of repetitive DNA sequences that occur at the ends of linear chromosomes in eukaryotes (such as humans and other mammals). In ...
8
votes
6
answers
949
views
Counting the sequences
I was thinking when SQL can be less verbose than traditional programming languages and came up with this challenge.
Input
Natural number N
Generating data
We ...
-7
votes
8
answers
314
views
Internationalisation -> I18n (numeronym)
Write the shortest program or function that accepts a sequence of three or more letters and returns its I18n-style numeronym, preserving the original letter case. The I18n-style numeronym is formed by ...
1
vote
2
answers
230
views
Create an autoclicker for Bands and Bonds
Bands and Bonds is a browser clicker game about delving in a dungeon. It has the particularity that only one of the actions you may take can be auto-repeated at the same time: You may start several ...
9
votes
3
answers
322
views
Divide 1 by a sum/difference of square roots
Divide 1 by a sum/difference of square roots
Input: An expression that is a sum/difference of square roots of positive integers. You can assme it will not equal 0.
The general form is \$\pm\sqrt{a_1}\...
5
votes
4
answers
357
views
DNA reading frame
DNA is read in groups of three nucleotides (the letters you see in a sequence), called codons, for determining what protein sequence the portion of DNA will yield. I already used this in a previous ...
6
votes
9
answers
698
views
Parse rpip7 musical notation
Parse rpip7 musical notation
rpip7 is a minimalist music notation derived from ip7, designed for simple playback (for example, on an Arduino with a buzzer).
It supports only these symbols:
...
21
votes
17
answers
1k
views
A yet-unlisted sequence
I was looking for a simple sequence that's not yet referenced on the OEIS and came up with this one(*):
\$a_1=2\$
\$a_2=3\$
For \$n>2\$, \$a_n\$ is the smallest number of the form \$a_i\times a_j+...
14
votes
17
answers
1k
views
Compute the NFL passer rating
Each quarterback in the NFL is given a passer rating at the end of the game, which measures how good their forward passes were. It is not strictly a basic arithmetic formula, and is calculated as ...
10
votes
8
answers
752
views
Closest Palindromic Number with Matching Smallest Valid Base
An extension of Find the closest palindromic number mixed with Numbers Interpreted in Smallest Valid Base.
A number is a palindrome if its sequence of digits is the same when reading them from left to ...
15
votes
18
answers
1k
views
Rebase Until Inert
This is a sequel to Numbers Interpreted in Smallest Valid Base.
Let us define the operation of rebasing a number as writing out its digits in decimal form, then interpreting them in the smallest base <...
19
votes
13
answers
2k
views
Interpret G-code
G-code is a set of instructions used to manipulate machines in CNC, 3d printing, or other similar applications. It controlls the position of a tool, manuvering it to create a path.
Your goal is to ...
20
votes
31
answers
2k
views
Numbers Interpreted in Smallest Valid Base
Let us define the operation of rebasing a number as writing out its digits in decimal form, then interpreting them in the smallest base N possible (...
-5
votes
14
answers
445
views
Text Flattening Program
Input: A string, can be multi-line. The newlines must to be real newline (e.g., you cannot request that each newline be (n)). There is another input, a number (not ...
15
votes
10
answers
2k
views
Verify a Chinese ID Number
Verify a Chinese ID Number
The Resident Identity Card (居民身份证) is a document issued to most Chinese citizens. It contains information about its holder, including their name, address, and ID number, an ...
11
votes
7
answers
739
views
On the Subject of Switches
This challenge is from a video game, Keep Talking And Nobody Explodes.
A yes or no choice isn’t too bad. Unfortunately you have to make five of them and any of them could be your last. — from the ...
12
votes
12
answers
748
views
Recursive cumulative sum [duplicate]
Challenge:
Given inputs \$i\$ and \$n\$, calculate \$R_i(n)\$ where:
$$R_0(n)=n \\
R_i(n)=\sum_{j=0}^nR_{i-1}(j)$$
Note that \$R_1\$ is triangular function, and \$R_2\$ is tetrahedral function.
This ...
10
votes
2
answers
370
views
Check if a number is a power with its program value being a power
Task:
Your task is make a program to check if there exists integers \$a \ge 2, b \ge 2\$ such that \$a^b=i\$ where \$i\$ is the input integer at least 2.
The catch is that your program must return a ...
8
votes
12
answers
2k
views
Return sum of last 3 odd numbers
Introduction
In my CS4 class I worked with another student to golf this lab as much as possible. I learned a lot about Ruby with it, and I am curious what optimizations could be done. :D
Challenge
A ...
11
votes
11
answers
795
views
Find a linear function such that if f(0), f(1), f(2), ⋯ are indices of the elements of the list, the sum must equal to its slope
Challenge:
Find integers \$a\$ and \$b\$ defining linear function \$f(x) = ax + b\$ with \$a > 0, b \ge 0\$ such that the sum of the elements of the input list \$l\$ located at the values of the ...
5
votes
13
answers
857
views
Which letter is missing? [closed]
Given a printable ASCII string, return any letter which is not present (ignoring case), if possible, else return the empty string.
Test Cases:
...
13
votes
4
answers
708
views
Continue the spiral
Write a program or function which, given a clockwise spiral as input, extends that spiral by one character.
When given no input, your function returns the start of the spiral:
...
18
votes
18
answers
1k
views
Peg solitaire starting position
Write a program/function to output a peg solitaire board in its starting position:
● ● ●
● ● ●
● ● ● ● ● ● ●
● ● ● ○ ● ● ●
● ● ● ● ● ● ●
● ● ●
● ● ●
...
17
votes
22
answers
2k
views
Find the first relabeling
I had this problem come up for me recently and I thought it would make a fun code-golf.
You will receive as input a list of positive integers. e.g.
...
8
votes
6
answers
839
views
How does the NSDT program end?
Challenge:
NSDT is like a calculus model invented by me that contains only 4 commands and grouping. Your task is to check in which way the program ends. The program is read from left to right. There ...
3
votes
4
answers
778
views
Emoji-art sunburst
Write a program or function to output an N x N sunburst image made of red, orange and yellow emoji icons.
For N=40, it should look roughly like this:
...
17
votes
10
answers
1k
views
Output the colour code of a given resistance
Should be relatively easy to do, given an input of a resistance (in ohms) and a tolerance (%), output the colour codes for a 4 band resistor, left to right. Input will always be a valid ohm value and %...
12
votes
2
answers
232
views
Autocomplete a word at the cursor position
You will be given a block of text and a cursor position at the end of a possibly incomplete word.
You must output a suggestion for what this word (let's call it w) ...
14
votes
20
answers
2k
views
Sum of Neighbours in a 2D Grid
This is the 2D version of Sum of neighbours: Given a 2D grid of integers, output a grid of the same size where each cell contains the sum of the values of its orthogonal and diagonal neighbors (up to ...
12
votes
9
answers
1k
views
Exit a pinball board
You will be given a pinball board with a ball on it. Determine where the ball will exit the board. The ball will fall straight down, unless obstructed by a left or right bumper, in which case it will ...
-4
votes
3
answers
345
views
Render a polygon...in ASCII [closed]
Given the anti-clockwise points of a properly formed, non-self-intersecting, not-necessarily-convex polygon, render it as a filled ASCII art polygon.
Input
A series of at least 3 (x,y) pairs ...
2
votes
1
answer
346
views
Code Golfing in Bash
I'm pretty new to code golfing, but I recently came across a guy on Kattis that consistently has exactly 5 character Bash solutions for some problems. For example, here is a leaderboard showing his 5 ...
12
votes
15
answers
2k
views
Unroll the loop!
A loop is a rectangle of text like this:
hgf
ile
jkd
abc
To unroll it, you start at the bottom left, and proceed to the right, spiralling anti-clockwise in towards ...
21
votes
10
answers
2k
views
"e" ejects the letters. Re-enter them
Overview
I have a sample of 10 quotes. You will receive as input to your program one of these quotes, however all the vowels (aeiou) will be replaced with the ...
17
votes
6
answers
884
views
Decompose a palindromic polynomial
A palindromic polynomial is a polynomial whose list of coefficients is a palindrome. For example, the polynomial \$p(x) = x^4 + 2x^3 + 3x^2 + 2x + 1\$ is palindromic because its coefficients are \$[1, ...
2
votes
4
answers
621
views
Program Generator: Print a Paragraph
Objective
Your challenge is to write a program that, given a paragraph from a book, will output another program that prints out that paragraph. (See my example program in an answer)
Scoring
The link ...
18
votes
30
answers
2k
views
Add Y to X to get a palindrome
Given X>0, return the smallest Y>=0 such that X+Y is a palindrome. Code golf.
Tests:
5 => 0
14 => 8
200 => 2
819 => 9
1100 => 11
1122 => 99
5
votes
3
answers
431
views
Cancel strings to make them short
Overview
You will be given a list of strings. If any two elements are equal, you can delete both of them. If one element a is a substring of element ...
13
votes
8
answers
1k
views
Float vs Decimal
You will be given a decimal number n in the form of a string. You must determine if that number, when stored in standard number type ...