Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take 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
606 votes
309 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....
user avatar
  • 33.7k
498 votes
913 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 ...
user avatar
389 votes
44 answers

Covfefify a string

In this challenge, you must take a string matching the regex ^[a-zA-Z]+$ or whatever is reasonable (you don't have to consider uppercase or lowercase letters if you ...
user avatar
368 votes
1 answer

Regex that only matches itself

There are some pretty cool challenges out there involving regex (Self-matching regex, Regex validating regex) This may well be impossible, but is there a regex that will ONLY match itself? NOTE, ...
user avatar
315 votes
168 answers

Tips for golfing in Python

What general tips do you have for golfing in Python? I'm looking for ideas which can be applied to code-golf problems and which are also at least somewhat specific to Python (e.g. "remove comments" is ...
user avatar
  • 12.2k
307 votes
7 answers

Draw with your CPU

I have come across an article where students used network traffic to draw their university on the country's IPv6 graph. [image] Your goal is simple to tell, but hard to implement. Draw the text MAIL (...
user avatar
  • 4,206
284 votes
73 answers

We're no strangers to code golf, you know the rules, and so do I

Write the shortest program that prints the entire lyrics of "Never Gonna Give You Up" by Rick Astley. Rules: Must output the lyrics exactly as they appear in the above pastebin*. Here's the raw dump:...
user avatar
  • 4,390
232 votes
411 answers

Golf you a quine for great good!

Using your language of choice, golf a quine. A quine is a non-empty computer program which takes no input and produces a copy of its own source code as its only output. No cheating -- that means ...
user avatar
  • 2,597
232 votes
27 answers


You have been hired for your tech knowledge as a Secret Agent's sidekick to ensure that the good guy can get his job done and the world can be saved. This is your last mission before retiring with a ...
user avatar
  • 4,039
231 votes
109 answers

Print every character your program doesn't have

Your task is to build a program (using only printable ASCII characters and/or tabs and newlines) that prints out exactly the characters in the printable ASCII space (...
user avatar
  • 33.7k
228 votes
335 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 ...
user avatar
  • 206k
218 votes
62 answers

No A, just CAPS LOCK

What happens when the CapsLock key on your keyboard doesn't have a notch in it? "This hPPENS." The goal of this program is to consistently emulate keyboard misses where each A press is replaced with ...
user avatar
  • 1,731
197 votes
212 answers

The Programming Language Quiz

Congratulations to Dennis who won both the cops' and the robbers' challenge! Calvin's Hobbies has already delivered on his promise and wrote this challenge for Dennis for winning the robbers' ...
user avatar
195 votes
120 answers

I'm not the language you're looking for!

Isn't it annoying when you find a piece of code and you don't know what language it was written in? This challenge attempts to somewhat solve this. Challenge You will have to write a program that ...
user avatar
  • 28.1k
192 votes
61 answers

Mathematics is fact. Programming is not

In mathematics an exclamation mark ! often means factorial and it comes after the argument. In programming an exclamation mark ! ...
user avatar
184 votes
379 answers

1, 2, Fizz, 4, Buzz

Introduction In our recent effort to collect catalogues of shortest solutions for standard programming exercises, here is PPCG's first ever vanilla FizzBuzz challenge. If you wish to see other ...
user avatar
  • 22.8k
182 votes
29 answers

Do you want to code a snowman?

Here is a simple ASCII art snowman: _===_ (.,.) ( : ) ( : ) Let's make him some friends. This will be the general pattern for our ASCII art snowpeople: ...
user avatar
168 votes
458 answers

Implement a Truth-Machine

A truth-machine (credits goes to this guy for coming up with it) is a very simple program designed to demonstrate the I/O and control flow of a language. Here's what a truth-machine does: Gets a ...
user avatar
  • 11.1k
167 votes
163 answers

Don't google "google"

We all know that if you google the word "google" it will break the internet. Your task is to create a function that accepts one string and returns its length, in the fewest possible Unicode ...
user avatar
  • 4,507
165 votes
127 answers

Trick or Treat polyglot

Since Halloween is coming up I thought I might start a fun little code golf challenge! The challenge is quite simple. You have to write a program that outputs either ...
user avatar
  • 1,531
165 votes
5 answers

Hyperprogramming: N+N, N×N, N^N all in one

Write a program that takes in a number N from 1 to 9 inclusive. In its native form your program should output N+N. E.g. output 2 if N is ...
user avatar
164 votes
91 answers

Tips for golfing in JavaScript

What general tips do you have for golfing in JavaScript? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to JavaScript (e.g. "remove ...
user avatar
  • 5,962
159 votes
59 answers

Tips for golfing in C

What general tips do you have for golfing in C? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to C (e.g. "remove comments" is not an ...
user avatar
  • 3,449
159 votes
18 answers

Is My Swipe Pattern Legal?

Most Android smartphones allow the user to use a swipe pattern to open their phone: Certain patterns are legitimate, and others are impossible. Given an input swipe pattern, return a truthy or falsy ...
user avatar
  • 1,131
154 votes
28 answers

Shortest Code to Legitimately Slack Off

I'm a developer, and I don't feel like doing my work. I know from XKCD that the best excuse for slacking off is that your code's compiling. Because of this, I think I need some code that will compile ...
user avatar
149 votes
1 answer

"KNOT" or "NOT"?

Write a program that processes an ASCII art representation of a tangled string and decides whether or not it can be untangled into a simple loop. The tangle is represented using the characters ...
user avatar
  • 19.6k
148 votes
154 answers

I double the source, you double the output!

Your task, if you wish to accept it, is to write a program that outputs a positive integer (higher than 0). The tricky part is that if I duplicate your source code, the output must be double the ...
user avatar
  • 41.4k
146 votes
245 answers

Unscramble the Source Code

Note: This challenge is now closed to new cop submissions. This is to ensure that no one can post submissions that only remain uncracked because there aren't enough robbers interested in the challenge ...
user avatar
  • 28.5k
145 votes
116 answers

Shortest Error Message

Challenge Write the shortest program that, when compiled or executed, produces a fatal error message smaller than the program itself. The error message may not be generated by the program itself, ...
user avatar
  • 1,473
144 votes
298 answers

Fibonacci function or sequence

The Fibonacci sequence is a sequence of numbers, where every number in the sequence is the sum of the two numbers preceding it. The first two numbers in the sequence are both 1. Here are the first ...
user avatar
141 votes
320 answers

Shortest code to produce infinite output

Write the shortest code you can that produces an infinite output. That's all. You code will only be disqualified if it stops producing output at some point. As always in code golf, the shortest code ...
user avatar
  • 2,376
140 votes
553 answers

Shortest infinite loop producing no output

Your task is to create the shortest infinite loop! The point of this challenge is to create an infinite loop producing no output, unlike its possible duplicate. The reason to this is because the code ...
user avatar
  • 12.8k
140 votes
87 answers

Hello, World! (Every other character)

Write a program that prints "Hello, World!". But also, if you take only the first, third, fifth, etc. characters of your program, the resulting program should still print "Hello, World!". If your ...
user avatar
  • 3,043
139 votes
27 answers

Appease Your Google Overlords: Draw the "G" Logo

Write a program or function that takes in a positive integer N, and outputs an N×N pixel image of Google's "G" logo according to this* construction: For example, if N is 400, a 400×...
user avatar
136 votes
66 answers

Interpret brainfuck

Write the shortest program in your favourite language to interpret a brainfuck program. The program is read from a file. Input and output are standard input and standard output. Cell size: 8bit ...
user avatar
  • 5,905
132 votes
136 answers

Obfuscated Hello World

Create the shortest possible obfuscated program that displays the text "Hello World". In order to be considered an obfuscated program, it must meet at least two of the following requirements: Does ...
user avatar
130 votes
31 answers

A keyboard so real you can almost TASTE it

... Not that you would, would you? The task is simple, output the following text: ...
user avatar
127 votes
200 answers

Sing Happy Birthday to your favourite programming language

Your favourite programming language has just had a birthday. Be nice and sing it the Happy Birthday song. Of course you should accomplish this by writing a program in that language. The program takes ...
user avatar
  • 2,021
123 votes
60 answers

Program that creates larger versions of itself (quine-variant)

You are to write a program that will output source code that is Larger than the original program (character wise) Will print another program larger than itself when run (i.e. the new program is also ...
user avatar
  • 7,069
123 votes
40 answers

La La Land... no wait, Moo Moo Moonlight

This challenge is a tribute to the winner of Best Picture at the Oscars 2017, La La Land Moonlight! Write a function/program that takes a string containing only letters ...
user avatar
118 votes
97 answers

Loading... Forever

Your challenge is to make an infinite loading screen, that looks like this: Or, to be more specific: Take no input. Output Loading..., with a trailing space, but ...
user avatar
  • 13.9k
117 votes
62 answers


Today is November 8th, 2016, Election Day in the United States of America. If you are a U.S. citizen eligible to vote, then go out and vote if you haven't already before answering this challenge. Do ...
user avatar
116 votes
19 answers

"Hello, World!" in zero lines of code

NPM's sloc is a moderately popular tool for counting source lines of code in a file. The tool will attempt to strip out both single and multiline comments and count the remaining lines in order to get ...
user avatar
  • 11.7k
115 votes
69 answers

I'm a palindrome. Are you?

There have been a couple of previous attempts to ask this question, but neither conforms to modern standards on this site. Per discussion on Meta, I'm reposting it in a way that allows for fair ...
user avatar
115 votes
47 answers

It's my Birthday :D

Introduction Last year was my birthday (really!) and sadly I had to organise my own party. Well, now you know, couldn't you at least make the cake? Challenge Given an integer ...
user avatar
115 votes
8 answers

Exponentially Slimy Programming: Stacking Minecraft Slimes

Slimes are cube shaped enemies in Minecraft that break into multiple smaller versions of themselves when killed. For the purposes of this challenge we'll depict them as an 8×8 pixel image with 3 ...
user avatar
113 votes
384 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 ...
user avatar
  • 48.2k
112 votes
78 answers

Shortest code that raises a SIGSEGV

Write the shortest code that raises a Segmentation Fault (SIGSEGV) in any programming language.
user avatar
  • 1,319
112 votes
32 answers

Blink the CAPS LOCK

Most computer keyboards feature a small integrated LED light, indicating the current input mode, as controlled with the CAPS LOCK button. Your task is to blink it: Turn it on; Wait for 0.5 (+/-0.1) ...
user avatar
  • 8,386
111 votes
11 answers

GottaFix for WannaCrypt?

Warning The answers to this challenge test for a specific version of the patch that helps stop WannaCrypt/WannaCry attacks. Depending on your operating system, you may have a different patch. The best ...
user avatar

2 3 4 5