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
44 votes
14 answers
14k views

Tips for golfing in 05AB1E

Do you have any tips for code-golfing in 05AB1E, a golfing language created by Adnan? Your tips should be at least somewhat specific to 05AB1E. Please post one tip per answer.
user avatar
  • 10.3k
315 votes
168 answers
118k views

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
102 votes
39 answers
25k views

Tips for golfing in Java

Are there any useful shortcuts that can be used in Java? As shown below, import already adds at least 17 characters to a program. ...
user avatar
  • 1,437
86 votes
55 answers
15k views

Tips for golfing in Haskell

What general tips do you have for golfing in Haskell? I am looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Haskell. Please post only one ...
user avatar
232 votes
411 answers
57k views

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
498 votes
913 answers
171k views

"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
101 votes
41 answers
11k views

Tips for golfing in <all languages>

The aim of this post is to gather all the golfing tips that can be easily applied to <all languages> rather than a specific one. Only post answers that its ...
user avatar
  • 3,608
159 votes
59 answers
33k views

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
164 votes
91 answers
59k views

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
49 votes
49 answers
7k views

Tips for golfing in PowerShell

What general tips do you have for golfing in Windows PowerShell? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to PowerShell (e.g. "...
user avatar
  • 12.8k
71 votes
55 answers
18k views

Tips for code-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
  • 1,783
228 votes
335 answers
58k views

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
69 votes
23 answers
6k views

Tips for golfing in R

I'm looking for tips for golfing in the R statistical language. R is perhaps an unconventional choice for Golf. However, it does certain things very compactly (sequences, randomness, vectors, and ...
user avatar
105 votes
42 answers
15k views

Tips for Golfing in ECMAScript 6 and above

This is similar to other "Tips for golfing in <...>" but specifically targeting the newer features in JavaScript brought up in ECMAScript 6 and above. JavaScript inherently is a very verbose ...
user avatar
  • 26.4k
48 votes
42 answers
6k views

Tips for golfing in Mathematica

What general tips do you have for golfing in Mathematica? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Mathematica (e.g. "remove ...
user avatar
  • 32.9k
75 votes
53 answers
28k views

Tips for golfing in Ruby

What general tips can you give for golfing in Ruby? I'm looking for ideas that can be applied to code golf problems in general that are specific to Ruby. (For example, "Remove comments" would not be ...
user avatar
  • 1,237
64 votes
41 answers
10k views

Tips for golfing in Bash

What general tips do you have for golfing in Bash? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Bash (e.g. "remove comments" is not ...
user avatar
  • 19.4k
284 votes
73 answers
77k views

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
40 votes
17 answers
4k views

Tips for golfing in APL

I started one code golf challenge recently and it seems like the winner is GolfScript (surprise, surprise!). What's interesting is that there was another very strong competitor that had all chances to ...
user avatar
  • 1,039
144 votes
298 answers
37k views

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
61 votes
108 answers
15k views

List of primes under a million

This is my first code golf question, and a very simple one at that, so I apologise in advance if I may have broken any community guidelines. The task is to print out, in ascending order, all of the ...
user avatar
184 votes
379 answers
64k views

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
51 votes
24 answers
13k views

Tips for golfing in Perl?

What general tips do you have for golfing in Perl? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Perl (e.g. "remove comments" is not ...
user avatar
  • 1,093
41 votes
39 answers
9k views

Tips for golfing in PHP

What general tips do you have for golfing in PHP? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to PHP (e.g. "remove comments" is not ...
user avatar
  • 3,422
103 votes
310 answers
20k views

Simple cat program

One of the most common standard tasks (especially when showcasing esoteric programming languages) is to implement a "cat program": read all of STDIN and print it to STDOUT. While this is named after ...
user avatar
56 votes
24 answers
15k views

Tips for golfing in Jelly

Jelly is a tacit, golf-oriented programming language by our very own Dennis. It’s popping up in answers here more and more often, beating other golf-y languages like Pyth and CJam, both by using its ...
user avatar
  • 65.5k
23 votes
12 answers
1k views

Tips for golfing in MATL

MATL is a golfing language created by Luis Mendo. MATL has proven to be highly competitive, often beating submissions in other golfing languages such as Pyth, CJam and Jelly. What are some useful ...
user avatar
45 votes
33 answers
6k views

Tips for golfing in x86/x64 machine code

I noticed that there's no such question, so here it is: Do you have general tips for golfing in x86/x64 machine code? If the tip only applies to a certain environment or calling convention, please ...
user avatar
  • 16.4k
60 votes
28 answers
17k views

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 ...
user avatar
  • 12.2k
41 votes
17 answers
5k views

Tips for golfing in GolfScript

What, this post doesn't exist yet? Of course, GolfScript is made for golfing, so you might think that no specific tips are really needed. But to make full use of GolfScript's features, you need to ...
user avatar
148 votes
154 answers
21k views

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
19 votes
33 answers
3k views

Tips for golfing in VBA

Similar to this, this, and this question... What general tips do you have for golfing in VBA? I'm looking for ideas that can be applied to code golf problems in ...
user avatar
  • 3,539
17 votes
8 answers
1k views

Tips for golfing in Whitespace

What general tips do you have for golfing in Whitespace? I'm looking for ideas which can be applied to code-golf problems and which are also at least somewhat specific to Whitespace (e.g. "remove ...
user avatar
  • 7,940
40 votes
17 answers
4k views

Golf a mutual quine

Using two languages of your choice, write the smallest “mutual quine” you can. That is, write a program P in language A that outputs the source code for a program Q in language B, such ...
user avatar
  • 6,521
13 votes
10 answers
2k views

Encode a program with the fewest distinct characters possible

The goal is to write a program that encodes an other program (input) with the fewest character possible. Scoring The score is equal to the different number of characters needed for the output. Lower ...
user avatar
66 votes
43 answers
5k views

Write the shortest self-identifying program (a quine variant)

Write a program that will generate a "true" output iff the input matches the source code of the program, and which generates a "false" output iff the input does not match the source code of the ...
user avatar
  • 28.5k
44 votes
34 answers
4k views

Tips for golfing in CJam

CJam is a GolfScript-inspired stack-based golfing language, created by PPCG user aditsu. So, in the vein of other language-specific tips questions: What general tips do you have for golfing in CJam? ...
user avatar
132 votes
136 answers
42k views

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
168 votes
458 answers
32k views

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
92 votes
165 answers
21k views

"99 Bottles of Beer"

Write a program that outputs the lyrics to 99 Bottles of Beer, in as few bytes as possible. Lyrics: ...
user avatar
  • 7,406
42 votes
64 answers
9k views

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 ...
user avatar
  • 7,682
90 votes
145 answers
12k views

Determine whether strings are anagrams

Challenge Given two strings, work out if they both have exactly the same characters in them. Example Input word, wrdo This returns true because they are the ...
user avatar
  • 1,209
28 votes
8 answers
3k views

Tips for golfing in Julia

What general tips do you have for golfing in Julia? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Julia (e.g. "remove comments" is ...
user avatar
606 votes
309 answers
230k views

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
20 votes
23 answers
2k views

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 ...
user avatar
  • 5,661
78 votes
250 answers
24k views

Is this even or odd?

Note: There is not been a vanilla parity test challenge yet (There is a C/C++ one but that disallows the ability to use languages other than C/C++, and other non-vanilla ones are mostly closed too), ...
user avatar
  • 5,271
23 votes
13 answers
4k views

Converting integers to English words

The goal of this code golf is to convert integers to English words. The program prompts for input. If this input isn't an integer, print NaN. If it is an integer,...
user avatar
  • 8,273
89 votes
199 answers
19k views

Find the Factorial!

Create the shortest program or function that finds the factorial of a non-negative integer. The factorial, represented with ! is defined as such $$n!:=\begin{...
user avatar
67 votes
226 answers
17k views

Add two numbers

Input: Two integers. Preferably decimal integers, but other forms of numbers can be used. These can be given to the code in standard input, as arguments to the program or function, or as a list. ...
user avatar
  • 6,217
60 votes
43 answers
6k views

Are the brackets fully matched?

You must write a program or function that takes a string of brackets and outputs whether or not that string is fully matched. Your program should print a truthy or falsy value, and IO can be in any ...
user avatar

1
2 3 4 5
75