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
8
votes
8answers
81 views

Calculate the inverse of a matrix

Somehow, we don't yet have a challenge for finding the inverse of an arbitrarily-sized square matrix, despite having ones for 3x3 and 4x4, as well as a more complex version. Your task is, given a ...
1
vote
0answers
70 views

Is a coincident point in a pair of rotated hexagonal lattices closest to the origin?

Preface This is a code-golf question where the technique must search or exclude from search all possibilities. As soon as I figure out how to pose it, there will be a separate code-challenge question ...
12
votes
5answers
274 views

What valence does this APL train have?

Context In APL, trains are tacit sequences of monadic/dyadic functions that can be called with one or two arguments. We'll code something to check if a given train follows the correct structure we ...
26
votes
14answers
1k views

Is it a digit haiku?

We can read 13576870234289 digit-by-digit in English, and it makes a haiku: one three five seven six eight seven zero two three four two eight nine We say it's a &...
14
votes
8answers
1k views

The square root of the square root of the square root of the…

This code-golf challenge will give you an integer n, and ask you to count the number of positive integer sequences \$S = (a_1, a_2, \dots, a_t)\$ such that \$a_1 + ...
7
votes
11answers
518 views

Output ISO8601 date string from seconds and nanoseconds

One reason why ISO8601 is the best date string format, is that you can simply append as much precision as you like. Given 2 integers representing seconds and nanoseconds that have passed since ...
23
votes
9answers
2k views

The Knight's Dialer

Imagine you place a knight chess piece on a phone dial pad. This chess piece moves from keys to keys in an uppercase "L" shape: two steps horizontally followed by one vertically, or one step ...
-7
votes
0answers
61 views

Chess: Is there a check? [duplicate]

You get a string representation of a chessboard. It consists of 64 characters going row wise from top left to bottom right. The figures are denoted with: K king Q queen R rook, castle B bishop N ...
8
votes
2answers
144 views

Unscramble the letters by letter frequency analysis [closed]

You know those riddles where every letter is swapped for another letter? E.g. you get a text like ...
15
votes
11answers
1k views

Rectangles in rectangles

This code-golf challenge will give you two positive integers n and k as inputs and have you count the number of rectangles with ...
19
votes
16answers
2k views

A Portuguese sequence of integers

Context Consider the following sequence of integers: $$2, 10, 12, 16, 17, 18, 19, ...$$ Can you guess the next term? Well, it is \$200\$. What about the next? It is \$201\$... In case it hasn't become ...
17
votes
5answers
460 views

Triangles with rational side lengths

This challenge will have give you a positive integer \$n\$ and ask you to output \$t(n)\$, the number of triangles (up to congruence) satisfying the three conditions: The triangles have perimeter of ...
20
votes
18answers
1k views

Eating Persistence

Intro Find the result of number cannibalism. Why was 6 scared of 7? Becase 7 8 9. Your program will have to find the result of applying the ...
1
vote
1answer
79 views

Simple “hyena” program [duplicate]

Write a program that takes a string as an input. For the input: If the input doesn't match the source code, the program shall output the input intact. (Hence a cat) Otherwise, the program shall fall ...
13
votes
19answers
2k views

Create an Accurate How-To Article

Here is an easy-intermediate challenge for anyone interested! What is that? A thing me and brother do a bit too often is this: One of us has a problem and asks the other to explain how to do certain ...
21
votes
19answers
913 views

Sequences of distinct positive integers

The goal of this challenge is to take a positive integer n and output (in lexicographic order) all sequences \$S = [a_1, a_2, ..., a_t]\$ of distinct positive ...
6
votes
21answers
1k views

Output all of printable ASCII using all of printable ASCII

Output all 95 bytes from 0x20 to 0x7E inclusive (printable ASCII including space), in any order, each at least once. The source ...
21
votes
13answers
2k views

Decide a C integer literal

Objective Given an ASCII string, decide whether it is a valid C integer literal. C integer literal A C integer literal consists of: One of: 0 followed by zero or ...
29
votes
20answers
3k views

Looks prime to me!

Figuring out whether a given number is prime, while not very complicated, is kind of hard. But making a guess doesn't need to be. Seeing whether a number is a multiple of 2 or 5 is easy - you can just ...
30
votes
31answers
2k views

Targeted duplicate removal

The task In this challenge, you are given a number and a list. Your task is to remove from the list all occurrences of the given number except the first (leftmost) one, and output the resulting list. ...
20
votes
42answers
3k views

Converting Strings to Decimals (Gone Wrong)

What? Many programming languages, both practical and esoteric, have functions, methods, etc to convert a given string to a decimal number. This is how the process might work: For each character in ...
11
votes
6answers
318 views

Geohash my positions

About geohash Geohash is one of many encoding systems for geographic positions. Geohash positions have some advantages. It provides a short code instead of the two usual numbers for latitude and ...
18
votes
13answers
963 views

Find all integer pairs that produce a given Loeschian number

Inspired by and drawns from Is this number Loeschian? A positive integer \$k\$ is a Loeschian number if \$k\$ can be expressed as \$i^2 + j^2 + i\times j\$ for \$i\$, \$j\$ integers. For example, ...
8
votes
2answers
205 views

Inverse quine: print everything except your program

Write a program which will eventually, given enough time, print all possible permutations of ASCII characters (both printable and non-printable) less than or equal to its length, except itself. The ...
7
votes
3answers
284 views

Shortest pristine truth machine

In this challenge, the goal is to create a pristine truth machine in as few bytes as possible. For reference, a truth machine does the following: Takes input, which is always 0 or 1 If the input is 0,...
37
votes
17answers
2k views

Three other numbers

Given three distinct numbers from \$1\$ to \$7\$, output three other distinct numbers from \$1\$ to \$7\$, that is having no numbers in common with the original numbers. Your code must produce a ...
21
votes
12answers
2k views

Find a divisibility pattern

Background Sometimes when I'm golfing a program, I'm presented with the following situation: I have an integer value \$x\$ on some fixed interval \$[a, b]\$, and I'd like to test whether it's in some ...
13
votes
6answers
1k views

Solve 4x4 Mastermind in exactly 5 steps

Background Mastermind is a game of code-breaking for two players. One of the players is codemaker (Alice) and the other is codebreaker (Bob). 4x4 means that the player should guess the length-4 ...
10
votes
5answers
538 views

Parse a Scala expression

Scala isn't a very commonly used language around here. Most of those who know it like it[citation needed], but some go :\ when they encounter its user-defined ...
19
votes
16answers
2k views

Size up a number

Intro Given a whole number \$< 100,\$ extend/shorten the english representation of the number to have as many characters as it's value. Sandbox Rules Take the number \$n\$, and get it in words ...
7
votes
8answers
563 views

Count Euler's Tours

Leonhard Euler wants to visit a few friends who live in houses 2, 3, ..., N (he lives in house 1). However, because of how his city is laid out, none of the paths between any houses form a loop (so, ...
6
votes
6answers
781 views

TTT: Turing Text Tape

Introduction Ah, INTERCAL... As much as I'd like encourage everyone to Try it Online, text output is just painful. According to the docs it uses the "Turing Text Model". While an... ...
14
votes
15answers
2k views

Palindrome distance

Find what is the distance for a given string to its closest palindrome of the same length. For this task I decided to give the characters further away from the string's center more weight (think of it ...
2
votes
4answers
415 views

Parse a nested parentheses

Objective Mimic Haskell's reads :: ReadS (). Or in other words, parse nested parentheses. Since the target type is () (the type ...
11
votes
1answer
374 views

Amount Of Potential Keys That'll Fit The Lock

Introduction: You're a key maker, and want to access something from a safe that has a lock. Unfortunately, the key to that lock is lost, so you'll have to make a new one. You have access to a bunch of ...
19
votes
36answers
4k views

Is that number a Two Bit Number™️?

Let's start by defining a Two Bit Number™️: It is a positive integer When expressed as a binary string it has exactly 2 true bits OR When expressed as a decimal number, it has exactly 2 of the ...
18
votes
5answers
2k views

How happy is this emoticon? (2.0)

OG post Task In this challenge, you will be given an string emoticon, and your task will be to output its happiness. How? An emoticon will always have eyes and mouth. It can also include eyebrows and ...
16
votes
27answers
2k views

Compactify the query string

Introduction Just on Hacker news, John Resig contemplates transforming a query “foo=1&foo=2&foo=3&blah=a&blah=b” into one that looks like this: “foo=1,2,3&blah=a,b", https://...
6
votes
2answers
299 views

Deserialize a binary tree breadth-first

Deserializing binary trees depth-first is pretty easy, but doing it breadth-first is (hopefully) harder. Your mission, should you choose to accept it, is to do the latter. The input will be a 1-D list ...
28
votes
22answers
3k views

Circumference of an ellipse

Challenge Unlike the circumference of a circle (which is as simple as \$2\pi r\$), the circumference of an ellipse is hard. Given the semi-major axis \$a\$ and semi-minor axis \$b\$ of an ellipse (see ...
13
votes
8answers
1k views

What logic gates work given the input?

Introduction Logic gates! Everyone knows them, everyone loves them (unless you're starting to learn them). Take two booleans, compare them, get a single boolean in return. ...
25
votes
19answers
3k views

Cat a mouse: ASCII art pointers

The Xerox Alto, originally released in 1973, was the first computer to feature the now-familiar angled mouse pointer. The Alto's bitmapped pointer looked like this (redrawn from Fig. 2 here): Your ...
14
votes
15answers
642 views

Conway's Game of Points!

Rest in peace, John Horton Conway There are a ton of different Game of Life interpreters out there! A ton! Currently for you, getting an interpreter is just a dozen types and a couple of clicks away. ...
15
votes
10answers
1k views

Output the International Phonetic Alphabet

The International Phonetic Alphabet is a system of phonetic notation intended to be able to accurately represent every sound used in spoken language. Unless I'm missing any, which I don't think I am, ...
2
votes
0answers
97 views

Grow in a slow-growing sequence [duplicate]

Background There is an interesting question on MathSE about some conjectures that are disproven by extremely large counter-examples. This delightful answer tells the story of a sequence of numbers ...
31
votes
9answers
2k views

Dice Paths on a Grid

You place a standard die at the origin of a 2D grid that stretches infinitely in every direction. You place the die such that the 1 is facing upwards, the 2 is facing in the negative y direction, and ...
15
votes
16answers
1k views

Concatenations of powers and their squares

At time of writing, my reputation is \$16,256\$. As I noted in chat, Oh cool my rep is the concatenation of two powers of 2: 16,256 Or even the concatenation of a power of 2 and its square, which is ...
-6
votes
1answer
89 views

No. Of Factors Does the Number Have [duplicate]

Create a function which takes in a input integer and then returns the number of factors it has, like if you pass 25 then it will return ...
13
votes
16answers
1k views

Plot a centered circle

Intro Given radius \$r\$, draw a circle in the center of the screen. Sandbox. The Challenge Here is a simple challenge. Plot a circle using the formula \$x^2+y^2=r^2\$, or any other formula that will ...
20
votes
23answers
1k views

Find the result of some assignment statements

In this challenge, the goal is to find the values of some variables after a number of assignments are done. An example input: a = 5 b = 4 c = a = b a = 2 b = a ...

1
2 3 4 5
200