Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

Hot answers tagged

96 votes

Tips for golfing in Python

Store lookup tables as magic numbers Say you want to hardcode a Boolean lookup table, like which of the first twelve English numbers contain an n. ...
user avatar
  • 137k
83 votes

Tips for golfing in Python

Collapse two numerical loops into one Say you're iterating over the cells of an m*n grid. Instead of two nested for loops, one ...
user avatar
  • 137k
74 votes

Tips for golfing in Python

For integer n, you can write n+1 as -~n n-1 as ...
user avatar
  • 137k
71 votes

Tips for golfing in C

Avoid catastrophic function-argument type declarations If you're declaring a function where all five arguments are ints, then life is good. you can simply write <...
user avatar
  • 30.8k
61 votes

How on earth did llhuii output the Evil Numbers in 42 bytes of Python?

This isn't the same solution as llhuii's, but it's also 42 bytes long. n=0;exec'print n;n^=(n^n+2)%3/2;n+=2;'*400 Try it online! Thanks to @JonathanFrech, we're ...
user avatar
  • 206k
59 votes

Tips for golfing in Python

Extended iterable unpacking ("Starred assignment", Python 3 only) The best way to explain this is via an example: ...
user avatar
  • 60.9k
58 votes

Tips for golfing in Java

Use interface instead of class. In java 8, static methods were added to interfaces. In interfaces, all methods are public by ...
user avatar
  • 11.4k
57 votes

Tips for golfing in <all languages>

Try to reduce logical statements For example, if A and B are booleans and your language treats booleans like numbers to some ...
user avatar
  • 2,880
56 votes

Tips for golfing in JavaScript

Shorter random number generation If you need a random boolean (0 or 1): ...
user avatar
  • 70.5k
53 votes

Tips for Golfing in ECMAScript 6 and above

Spread operator ... The spread operator transforms an array value into a comma separated list. Use case 1: Directly use an array where a function expects a list ...
user avatar
  • 31.8k
48 votes

Tips for golfing in Python

For ages it bothered me that I couldn't think of a short way to get the entire alphabet. If you use range enough that R=range is ...
user avatar
46 votes

Tips for golfing in <all languages>

Write an explanation of your code Writing an explanation forces you to thoroughly look at each part of your code again and to make your thoughts and choices in writing a certain passage explicit. In ...
user avatar
  • 25k
46 votes

Tips for golfing in Bash

Undocumented, but works in every version I've run into for legacy sh backwards compatibility: for loops allow you to use ...
user avatar
42 votes

How on earth did llhuii output the Evil Numbers in 42 bytes of Python?

Getting 39 bytes This is an explanation of how I got a 39-byte solution, which Dennis and JonathanFrech found separately as well. Or, rather, it explains how one could arrive at the answer in ...
user avatar
  • 137k
40 votes

Tips for golfing in <all languages>

Use unary ~ for x+1 and x-1 This trick applies to languages that have a unary bitwise ...
user avatar
  • 65.5k
40 votes

Smallest python script to print even numbers 0 to 100

Python 2 - 12 characters print 8**999 The decimal representation of all even numbers from 0 to 100 can be found in the output: ...
user avatar
  • 30.8k
39 votes

Tips for golfing in Python

Choosing one of two numbers based on a condition You already know to use the list selection [x,y][b] with a Boolean b for the ...
user avatar
  • 137k
39 votes

Tips for golfing in Python

Use ~ to index from the back of a list If L is a list, use L[~i] to get the i'th element ...
user avatar
  • 137k
39 votes

Tips for golfing in Python

Although python doesn't have switch statements, you can emulate them with dictionaries. For example, if you wanted a switch like this: ...
user avatar
  • 20.9k
39 votes

Tips for golfing in C

Use lambdas (unportable) Instead of f(int*a,int*b){return*a>*b?1:-1;} ... qsort(a,b,4,f); or (gcc only) ...
user avatar
  • 6,797
36 votes
Accepted

How can I use cmp(a,b) with Python3?

Python 3 does not have cmp. For golfing, you can do 11 chars (a>b)-(a<b) which loses 3 chars over ...
user avatar
  • 137k
36 votes

Tips for golfing in 05AB1E

Since it wasn't part of the Wiki on 05AB1E's GitHub pages (I think it should), I'm just gonna add it here now that I better understand it myself. How to use the dictionary? 05AB1E has the following ...
user avatar
35 votes
Accepted

Tips for Creating/Maintaining a Golfing Language

Here are some suggestions. Sorry that this partially overlaps with other answers, which have been posted as I was writing this. Design process One possibility (by all means not the only one) to ...
user avatar
  • 99.4k
34 votes

Tips for golfing in GolfScript

Rational / Float / Complex I've read so many times that GolfScript has only integers that I started to believe it. Well, it's not true. ...
user avatar
  • 206k
34 votes
Accepted

How do I alias member functions in Python?

No problemo! You can alias a method, but you have to know how to use it: ...
user avatar
  • 4,909
33 votes

Tips for golfing in Python

Ceil and Floor If you ever want to get the rounded-up result for a division, much like you'd do with // for floor, you could use ...
user avatar
  • 1,466
33 votes

Tips for golfing in JavaScript

Unicode shortcuts If you use a hell of a built-in property at a big golfing challenge you can alias every property to a one character equivalent: ...
user avatar
  • 4,206
33 votes

Tips for golfing in Jelly

String compression If you're looking for a more optimized/automatic string compressor, try this one. Try it online! A compressed string looks like “...», where ...
user avatar
  • 65.5k
31 votes

Tips for golfing in Python

PEP448 – Additional Unpacking Generalizations With the release of Python 3.5, manipulation of lists, tuples, sets and dicts just got golfier. Turning an iterable into a set/list Compare the pairs: ...
user avatar
  • 60.9k
31 votes

Smallest python script to print even numbers 0 to 100

Python 3, 22 (Possibly not allowed) If the challenge is "to create a python script which prints the even numbers from 0 to 100" and not "to create a python script which prints the even ...
user avatar
  • 41.3k

Only top scored, non community-wiki answers of a minimum length are eligible