Skip to main content
Share Your Experience: Take the 2024 Developer Survey

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
49 votes
40 answers

Last odd digit of power of 2

Task Given \$n\$, output position of the last odd digit in the decimal representation of \$2^n\$ (counting from the end). Rules There are no odd digits for \$n=1,2,3,6,11\$ \$(2, 4, 8, 64, 2048)\$ - ...
21 votes
8 answers

A cube of text\${}\$

Last time you made a square of text, but now, can you make a cube of text? The Challenge Given a string, output the string in the form of a cube. You can assume the string will always have 2 chars ...
6 votes
5 answers

Compare two strings, with NaC

I just thought of this idea: if we have NaN ("not a number") for floats, how about NaC ("not a character") for a single unknown character in a string? Your input should be two ...
18 votes
11 answers

Cold Bee and Old Beer

Introduction: Inspired by this silly meme video, which states 'COLD BEER', but is slightly too large for the scrolling advertisement board, causing it to display either 'COLD BEE' or 'OLD BEER' ...
26 votes
24 answers

I'm Lazy*: Top-left align my text

* and don't have a word processor with top-left align support :D Take several lines of input, with at least four unique characters of your choice including newline and space. The input can also be ...
19 votes
23 answers

Generate recursive "arrays" up to a certain depth

Given a positive integer n. Generate a JSON array (can be a string, or your language's built-in JSON representation as long as we can get valid JSON, (your code ...
82 votes
84 answers

Print some JSON

This challenge is straightforward, but hopefully, there are plenty of avenues you can approach it: You need to print/return a valid JSON object of at least 15 characters, not counting unessential ...
26 votes
17 answers

Make a Unicorn interpreter

Recently, I made a made a typo and I wrote unicorn instead of unicode, I did what any normal person would do and I made an esolang out of it. In this challenge you will be writing a Unicorn ...
13 votes
37 answers

First-n Fibonacci sequence elements

There is a well known question here that asks for a short (least characters) fibonacci sequence generator. I would like to know if someone can generate the first N elements only, of the fibonacci ...
236 votes
369 answers

Is this number a prime?

Believe it or not, we do not yet have a code golf challenge for a simple primality test. While it may not be the most interesting challenge, particularly for "usual" languages, it can be nontrivial in ...
23 votes
30 answers

Apply gravity to this matrix

This was inspired by this question. Given an \$m\times n\$ matrix of \$0\$'s and \$1\$'s, apply "gravity" to it. This means to drop down all the \$1\$'s as if they were affected by gravity. ...
14 votes
5 answers

Is this a solvable Numberlink?

Challenge Per Wikipedia: Numberlink is a type of logic puzzle involving finding paths to connect numbers in a grid. The player has to pair up all the matching numbers on the grid with single ...
122 votes
411 answers

Print a 10 by 10 grid of asterisks

Background This is a standard textbook example to demonstrate for loops. This is one of the first programs I learnt when I started learning programming ~10 years ago. Task You are to print this exact ...
520 votes
984 answers

"Hello, World!"

So... uh... this is a bit embarrassing. But we don't have a plain "Hello, World!" challenge yet (despite having 35 variants tagged with hello-world, and counting). While this is not the most ...
15 votes
5 answers

Recognize ASCII art numbers

Challenge Recognize ASCII art numbers. To make things interesting, three random points in the image might be flipped. For example: ...
3 votes
0 answers

Implement the simplest functional programming language

The goal of this challenge is to compare how well different programming languages support functional programming, by seeing how much code it takes to implement BLC8, the simplest functional ...
42 votes
70 answers

Infinitely many ℕ

Background: A sequence of infinite naturals is a sequence that contains every natural number infinitely many times. To clarify, every number must be printed multiple times! The Challenge: Output a ...
113 votes
88 answers

Draw the national flag of Iceland

This year's UEFA Euro 2016 is over and besides a couple of negative headlines there has been a very positive surprise as well – the Iceland national football team. Let's draw their national flag. ...
11 votes
22 answers

Decode Caesar cipher based on a given text

I can't think of a better name. This is similar to this challenge, but a lot easier. Read three strings which contain only uppercase letters. The length of the first and second strings are the same. ...
14 votes
9 answers

Truth Tables: Your great-grandfather's computer

If you remember back to your schooling years, you might remember learning about Truth Tables. They seemed boring, but they are basis for logic and (some would argue) all computing... Problem Your ...
55 votes
9 answers

Which tetromino is this?

Given an unsigned 16-bit integer N, your task is to determine whether its binary representation mapped inside a 4x4 matrix is matching a tetromino shape, and if so, which shape it is. Matrix Each ...
24 votes
27 answers

Tips for golfing in sed

What general tips do you have for golfing in sed? I'm looking for ideas which can be applied to code-golf problems and which are also at least somewhat specific to sed (e.g. "remove comments" is not ...
10 votes
1 answer

Radiation is Painful

A radiation-hardened quine can take a hit and stay standing, but nobody ever said that the quine can't be a wuss about it. For this challenge, you should write a quine that: If no characters are ...
5 votes
3 answers

Japanese Mahjong Score Calculator

I'm new to Japanese Mahjong and have trouble calculating the scores. Please help me write a program to calculate it. Introduction (aka you can skip this boring part) Mahjong is a popular tile game in ...
32 votes
32 answers

Javascript left-pad golf

Introduction In the wake of the left-pad npm package fallout, let's have a code golf for implementing left-pad. The left-pad function consists of 2 default arguments and 1 additional argument, in ...
12 votes
1 answer

Make a Frogger Solver

🐸🐸 You must make the shortest program to always find the optimal solution to a simplified Frogger game on a 9x9 grid. Course Elements: L - Log (Length: 3-4). ...
9 votes
14 answers

Convert CSV tables to MediaWiki tables

Input a CSV table, like this: data00,data01,data02,...,data0m data10,data11,data12,...,data1m ... datan0,datan1,datan2,...,datanm The cells in the CSV table is ...
93 votes
247 answers

"DDoouubbllee ssppeeaakk!!"

Super simple challenge today, or is it? I feel like we've heard a fair bit about double speak recently, well let's define it in a codable way... Double speak is when each and every character in a ...
14 votes
1 answer

Simulate Alpha Decay

Alpha Decay is a kind of radiation decay where every time the atom "decays", it loses a helium nucleus (an alpha particle). This means that an atom loses 2 protons and 2 neutrons each time. ...
27 votes
25 answers

Code Golf: 6174 - Kaprekar's mythical constant

Why is the number 6174 so interesting? As defined by Wikipedia Take any four-digit number, using at least two different digits. (Leading zeros are allowed.) Arrange the digits in ascending ...
13 votes
3 answers

Can this polyomino tile the toroidal grid?

Inspired by certain puzzles on Flow Free: Warps. Background We all know that L-triominos can't tile the 3x3 board, and P-pentominos can't tile the 5x5 board. But the situation changes if we allow the ...
15 votes
9 answers

Zip ragged lists

In Haskell (and probably some other languages or something) zip is a function which takes two lists, and produces a list of tuples by pairing elements at the same ...
9 votes
4 answers

Score a Scrabble Play

Background In Scrabble, players take turns placing tiles on a grid so that each contiguous set of (more than one) tiles in every row and column makes a word. In one play, tiles can be placed anywhere ...
5 votes
6 answers

Shrink ASCII art

The inverse of this question. Your challenge is to, given a piece of ASCII art generated by the above question and its enlargement factor, shrink it down to its original size. The enlargement process ...
26 votes
9 answers

Is it a Valid Crossword Grid?

In crossword terminology, the grid is the region into which the crossword answers are inserted, consisting of white and black squares. The crossword answers, called entries, are inserted into ...
17 votes
11 answers

Find the J twin

For a 2 dimensional array we will define the elements in either the first row or the last column to be the largest "J-Bracket" of the array. For example in the following array elements in ...
11 votes
4 answers

Starbucks to English

In the classic days of Starbucks before these newfangled sticker-printers and wireless drivethrough headset palmtop scanner dealies, in the heady days of 2000-ish, Starbucks had a concise system for ...
10 votes
4 answers

Count the corners, edges and faces of a cut cube

Coming from this sandbox post This is inspired from an 8th graders math test Intro We have a cube with following corners ...
13 votes
10 answers

Count the number of fields in each csv record

Imagine a text file where each csv record may have different numbers of fields. The task is to write code to output how many fields there are in each record of the file. You can assume there is no ...
0 votes
3 answers

Output in the Correct Order

Ok lets say we get a input the following csv file (all entries are quoted strings) ...
2 votes
10 answers

Shortest way to strip the commas from column n given a .csv file/input

Ok, so the input here is going to be a .csv file and so is the output. Now, given a .csv file/input like so ...
47 votes
11 answers

Monday Mini-Golf #6: Meeesesessess upp teeexexextext

Monday Mini-Golf: A series of short code-golf challenges, posted (hopefully!) every Monday. True story1: The other day, I was playing around on my tablet when I had the idea to visit the page I ...
49 votes
107 answers

Construct the Identity Matrix

The challenge is very simple. Given an integer input n, output the n x n identity matrix. The identity matrix is one that has <...
13 votes
11 answers

*Trivial* near-repdigit perfect powers

Task Output the sequence that precisely consists of the following integers in increasing order: the 2nd and higher powers of 10 (\$10^i\$ where \$i \ge 2\$), the squares of powers of 10 times 2 or 3 (...
6 votes
1 answer

Further golfing this C# code from Scramble words while preserving their outlines

I'll start off by saying I checked and checked again that this question should be on topic. I will also clearly state that this is a tips question about further golfing a certain set of code not a ...
627 votes
333 answers

Produce the number 2014 without any numbers in your source code

Note to challenge writers as per meta consensus: This question was well-received when it was posted, but challenges like this, asking answerers to Do X without using Y are likely to be poorly received....
12 votes
15 answers

Convert a string to sentence case

Input: A string (or the closest thing to that in your language) Output: The same string but converted to sentence case Rules: For this challenge, sentence case can be considered "...
25 votes
35 answers

Pascal's Triangle (Sort of)

Most everyone here is familiar with Pascal's Triangle. It's formed by successive rows, where each element is the sum of its two upper-left and upper-right neighbors. Here are the first ...
44 votes
69 answers

Generate Pascal's triangle

Pascal's triangle is generated by starting with a 1 on the first row. On subsequent rows, the number is determined by the sum of the two numbers directly above it to the left and right. To ...
21 votes
22 answers

Checkered grid with X mark

Challenge Given two integer values \$a \ge 2\$ and \$0 \le b < a\$, generate a \$(2a-1) \times (2a-1)\$ matrix consisting of the integers 0, 1, and 2 as follows: Create a checkerboard of 0s and 1s ...

2 3 4 5