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
1 vote
4 answers
14 views

Divisor of a string

Challenge Given a positive-length string \$S\$, a divisor of the string \$S\$ is when there exists a number \$a\$ so when we write the string \$a\$ times we get the string \$S\$. For example, the ...
user avatar
  • 847
9 votes
16 answers
126 views

Cartesian - polar conversion couple

We don't have a challenge for conversion between Cartesian and polar coordinates, so ... The challenge Write two programs (or functions) in the same language: one that converts from polar to ...
user avatar
  • 99.8k
16 votes
20 answers
1k views

Anti-divisors of a number

Given a positive integer n, output all of its anti-divisors in any order. From OEIS A006272: Anti-divisors are the numbers that do not divide a number by the ...
user avatar
  • 64.4k
19 votes
13 answers
1k views

Add parentheses to Polish notation

In most programming languages, arithmetic is written with infix notation -- i.e. the operator is put in between the operands -- e.g. 1+2. In contrast, with Polish ...
user avatar
  • 1,224
1 vote
5 answers
177 views

Find number components with lowest distribution

Let us assume that we have number X. Let us assume that we have positive integer "components" (C) of this ...
user avatar
  • 143
4 votes
3 answers
853 views

Python: can I shorten this code further for checking substrings

So the golf question was like this: You are given a number T and then a total of T number of 17-character strings. For each of ...
user avatar
15 votes
33 answers
2k views

Implement String Projection

Given two strings: a string s and an alphabet a, implement string projection in the shortest code possible. String projection ...
user avatar
  • 3,223
5 votes
1 answer
656 views

Help me golf this C program checking for string patterns in a sorted hand of cards

I could not think of a better title. This is a tips question. I'm asking for tips on golfing my code. I recently took part in a code golf competition in C. The competition ended and I lost. The ...
user avatar
  • 3,816
4 votes
9 answers
1k views

PUT-PIXEL in every language

During work on some project I needed to draw pixels on screen. I found some put-pixel function in JS, but code was quite big and clumsy - so I wonder, how small put-pixel function can be... Challenge ...
user avatar
8 votes
5 answers
696 views

Tips for golfing in Knight

Knight is the Language of the Month for August 2022, and I noticed that it didn't have a tips page, so here it is! I'm looking for ideas which can be applied to code-golf problems and which are also ...
user avatar
  • 5,523
18 votes
8 answers
1k views

Cryptic Multiplications

Given two non-negative integers e.g. 27, 96 their multiplication expression would be 27 x 96 = 2592. If now each digits is ...
user avatar
  • 2,153
15 votes
5 answers
1k views

Find The Average String Between Two Strings

Inspired by this Your task today: given two strings, find the string with the lowest maximum Levenshtein distance to the input strings. For example, using Steffan ...
user avatar
  • 3,317
6 votes
4 answers
368 views

Train Route Planning

We can model a rail network as a directed graph, where each node is a train station and each edge is a train connecting two train stations. We'll assume that each train leaves go between their ...
user avatar
  • 3,137
22 votes
16 answers
2k views

Remove redundant parentheses

Given a string containing some parentheses and some other ASCII printable characters, like this: (abc((123))(k)) your task is to remove any sets of parentheses that ...
user avatar
  • 1,593
12 votes
7 answers
287 views

Encode the systematic binary Varshamov–Tenengolts code

Background Here on CGCC, a commonly seen subgenre is radiation-hardening – writing a program that works even if one character is deleted. A similar problem is studied in the field of coding theory: ...
user avatar
23 votes
19 answers
3k views

Transform characters of your choice into "Hello, world!"

Write a program or function that takes a character as input and outputs a character. Also, choose a list of 13 distinct ASCII printable characters (32-126). When a character from this list is passed ...
user avatar
  • 1,195
40 votes
11 answers
2k views

IE's Extra Robust Color Parsing®

Recently, I've found a rant from 2004 about Internet Explorer's color parsing. Turns out, MS made sure that just about anything will be parsed as a color somehow and I think it makes for a neat ...
user avatar
17 votes
21 answers
2k views

Generate all Possibilities of Words

This is not the same as m-ss-ng-lette-s Challenge: Given a string with masked characters, generate all possible words, inclusive of non-meaningfull words. Rules: The sequence of the characters in the ...
user avatar
10 votes
36 answers
3k views

Repeat Hello World according to another string's length

This was related to a program I am writing. Although the title is simple, the challenge is a lot more complicated. Your challenge You must write a program that takes an input, and your answer must ...
user avatar
7 votes
4 answers
253 views

Generate number set with conditions using n numbers

Generate \$T=\{T_1,...,T_x\}\$, the minimum number of \$k\$-length subsets of \$\{1,...,n\}\$ such that every \$v\$-length subset of \$\{1,...,n\}\$ is a subset of some set in \$T\$ Here, \$n > k &...
user avatar
  • 179
20 votes
26 answers
2k views

Triangle area from side lengths

Output the area \$A\$ of a triangle given its side lengths \$a, b, c\$ as inputs. This can be computed using Heron's formula: $$ A=\sqrt{s(s-a)(s-b)(s-c)}\textrm{, where } s=\frac{a+b+c}{2}.$$ This ...
user avatar
  • 138k
9 votes
5 answers
307 views

Find the closest swap

Take as input two strings \$A\$ and \$B\$. Output a string \$C\$ that is \$A\$ but with two characters swapped such that the Levenshtein distance \$d(C,B)\$ is as small as possible. You must swap ...
user avatar
  • 89.4k
7 votes
7 answers
802 views

Swap every two elements in the list every possible way

Inspired by this question. Challenge Let L be a list of n distinct elements. Let P be the ...
user avatar
  • 89
31 votes
13 answers
4k views

Have you heard of tralindromes?

I haven't. :P So I decided to define one: A tralindrome is a string which contains three equal-length sections X, Y, Z such that all characters in the string are included in at least one of the three ...
user avatar
  • 64.4k
12 votes
12 answers
289 views

Runs of Ones (What Fun!) [duplicate]

Suppose you have an array with some known set of values (e.g. a string of \$0\$ and \$1\$) and you want to get all the locations of \$1\$s. Instead of storing a list of all the indices, if the \$1\$s ...
user avatar
  • 1,224
18 votes
37 answers
2k views

Shifted auto-sum

Let’s take a positive integer such as 123. We define the shifted auto-sum of this integer as follows: 123 has 3 digits. We thus consider 3 copies of 123. We stack ...
user avatar
  • 36.5k
19 votes
22 answers
2k views

Rearrange to a palindrome

Given a string, shuffle it so that it becomes a palindrome. For example, adadbcc can be arranged into dacbcad, or ...
user avatar
  • 31.5k
12 votes
6 answers
733 views

Exponential transform of an integer sequence

The exponential generating function (e.g.f.) of a sequence \$a_n\$ is defined as the formal power series \$f(x) = \sum_{n=0}^{\infty} \frac{a_n}{n!} x^n\$. When \$a_0 = 0\$, we can apply the ...
user avatar
  • 35.8k
19 votes
18 answers
2k views

Multiply numbers by their depth

Given a ragged list, we can define an element's depth as the number of arrays above it, or the amount that it is nested. For example, with the list ...
user avatar
  • 31.5k
21 votes
24 answers
2k views

Generate an arbitrary half of a string

Given a string \$ x \$, we say another string \$ y \$ is half of it, if both of the following properties are true: \$ y \$ is a (not necessarily continuous) subsequence of \$ x \$ - there exists a ...
user avatar
16 votes
9 answers
4k views

Super Mario 64 RNG

Introduction Super Mario 64 has a heavily overcomplicated RNG, ably explained here by Pannenkoek. I thought it might be interesting to implement it. Challenge Implement the RNG function, except for ...
user avatar
20 votes
23 answers
1k views

Letters to numbers to letters (poorly)

Every now and again, I'll encounter a large string of alphabetical text encoded as numbers from 1-26. Before I knew that I could just google a translator online, I had to personally, painstakingly ...
user avatar
8 votes
1 answer
401 views

Next Special String

Special String We call a binary string \$S\$ of length \$N\$ special if : substring \$S[0:i+1]\$ is lexicographically strictly smaller than substring \$S[i+1:N]\$ for \$ 0\leq i\leq N-2\$, Note: \$ ...
user avatar
  • 599
10 votes
3 answers
317 views

Tile stacking with ■ and □

In this challenge you will receive as input a list of binary lists. The list represents a game board with each element representing a location on the board. The list at each location represents the ...
user avatar
  • 89.4k
16 votes
13 answers
1k views

Count the cells

Given a multi-line string as input containing characters "|", "_" and ...
user avatar
  • 36.5k
19 votes
20 answers
3k views

Next digit of rational number

Story: The π was recently computed with accuracy to 100 trillions digits, but it is useless to us. We can't do accurate enough math, because rational numbers are too boring and so we don't know that ...
user avatar
  • 1,195
19 votes
15 answers
2k views

Infinite quote escaping sequence

Related We start with the string a, and forever append to the string a comma followed by the string quote-escaped, where quote-escaping means doubling all quotes in ...
user avatar
26 votes
15 answers
3k views

All Possible Ties in Tic-Tac-Toe

Your task is to output all possible ways to end a game with a tie (all rows, columns, and diagonals are completely filled and do not have 3 X's or 3 O's, and there are exactly 5 X's and 4 O's in total)...
user avatar
  • 2,141
13 votes
13 answers
1k views

Extract Strings from Text

In some languages, strings are started and ended with a quote mark ('). And quote itself is escaped by writing it twice sequentially. For example, empty string is ...
user avatar
  • 30.6k
11 votes
11 answers
998 views

Convert Descriptive Notation to Algebraic Notation

Introduction Many old chess books use descriptive notation to notate squares on a chessboard, naming the file (column) of the square based on the starting piece positions of White or Black, depending ...
user avatar
  • 11.2k
19 votes
23 answers
2k views

Every possible pairing

Given an positive even integer \$ n \$, output the set of "ways to pair up" the set \$ [1, n] \$. For example, with \$ n = 4 \$, we can pair up the set \$ \{1, 2, 3, 4\} \$ in these ways: \$...
user avatar
  • 22.2k
12 votes
8 answers
1k views

Solve a Card Suit Puzzle

Warning: Contains minor Deltarune Chapter 1 Spoilers Within the Forest section of the first chapter of Deltarune, there are a few puzzles that consist of entering playing-card suits in a certain order ...
user avatar
  • 22.4k
0 votes
0 answers
71 views

Invert colors given a hex color code [duplicate]

So the challenge is, given a string containing a hex color code, write a function in any programming language that takes the string and inverts the color, e.g., ...
user avatar
24 votes
30 answers
3k views

Backronymiser឵឵

Background A backronym is an acronym that was formed from an existing word. For example, spam is actually named after the canned meat product as used in the Monty Python sketch, but can be ...
user avatar
  • 22.2k
21 votes
9 answers
2k views

Simplify Rubik's Cube Moves

Background On a Rubik's cube there are 54 moves that you can execute, for example, turn the right face anti-clockwise, or rotate the top face and the horizontal slice twice. To notate any move, each ...
user avatar
6 votes
7 answers
771 views

Typo resistant pin codes [closed]

Background I was working on a system where for convenience (not security) people could use four digit codes to identify themselves. I figured this is something that may actually be useful in many real ...
user avatar
11 votes
3 answers
353 views

Unique rainbow programs

Given a list of positive integers as input you are to determine if there is a way to pair up the elements such that: All but one element is in a pair No element is in more than one pair Every pair is ...
user avatar
  • 89.4k
12 votes
16 answers
2k views

Squash it ... again!

If you place the positive integers together and read each set of two adjacent digits at the same time, you get: (A136414) ...
user avatar
  • 4,621
5 votes
7 answers
610 views

Convert from Greeklish to modern Greek

Greeklish, a portmanteau of the words Greek and English, is a way of writing modern Greek using only ASCII characters. This informal way of writing was extensively used in older applications / web ...
user avatar
  • 857
16 votes
13 answers
1k views

Implement pow with overflow checking

Implement a function or program which raises x to the power of y. Inputs are 16-bit signed integers. That is, both are in the ...
user avatar
  • 12.2k

1
2 3 4 5
225