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 ...
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 ...
53
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 ...
45
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 ...
43
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)
...
41
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 ...
41
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 ...
36
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 ...
35
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 ...
32
votes
Tips for restricted source in Python
Avoid "normal" letters
Identifiers are normalized by the Python 3 parser. This implies that cursive (Unicode) letters such as 𝓪𝓫𝓬𝓓𝓔𝓕 are interpreted ...
29
votes
Accepted
How to solve the LCM in 50 bytes of Python
My solution to 50 bytes:
v=1
for x in input().split()*6**6:v+=-v%int(x)
print(v)
Explanation
The solution itself is relatively simple, but finding it was ...
28
votes
Tips for golfing in Python
Assignment expressions
Assignment expressions are a powerful language feature introduced in Python 3.8 (TIO). Use the "walrus operator" := to assign a variable ...
28
votes
27
votes
Tips for golfing in Python
Common helper functions
These are some golfed implementations of number theoretic functions that come up in challenges a lot. Many of these are due to xnor, especially the “Wilson’s theorem prime ...
26
votes
Tips for code-golfing in C#
Instead of
bool a = true;
bool b = false;
do
var a=0<1;
var b=1<0;
If you need multiple variables, use this (...
26
votes
Tips for Golfing in Brain-Flak
Use the Third Stack
If you have read the title you might be a bit confused. Surely there are only two stacks in Brain-Flak? However I assure you that it exists and it is one of the most powerful if ...
25
votes
Tips for golfing in Python
Printing a string without a trailing newline in Python 3
Suppose you have a string s, and need to print it without a trailing newline. The canonical way of doing ...
25
votes
Tips for golfing in Python
The shortest infinite for comprehension
You may know the trick to easily create an infinite generator using the two-argument form of iter:
...
25
votes
Tips for golfing in JavaScript
Array sum / product / quotient
ES5: 17 bytes
eval(a.join('+'))
ES6: 15 bytes
eval(a.join`+`)
Of course you can swap the <...
25
votes
Tips for golfing in <all languages>
use - instead of !=
for numeric comparisons:
If a equals b, a-b results in ...
25
votes
Tips for golfing in Mathematica
Lists with repeated values
This is quite a common vector to work with:
{0,0}
It turns out this can be shortened by a byte:
...
25
votes
Accepted
Is it possible to make a clamp function shorter than a ternary in JS?
20 bytes
For reference, this is the original version without whitespace and without naming the function:
n=>n>0?n<255?n:255:0
Try it online!
19 bytes
We ...
25
votes
Tips for restricted source in Python
Avoid Numbers with Booleans
When performing arithmetic operations on Booleans, Python treats them as if they are the numbers 1 and 0. So for example
...
Community wiki
25
votes
Accepted
Can `a?b<c:b>c` be shortened in Javascript?
Yes, it can be shorter!
c-b<a^a beats a?b<c:b>c by 2 bytes. My tests show that it works for any integer, including ...
24
votes
Tips for golfing in JavaScript
Abuse literals
The recent sample: Check whether "c" is uppercase or lowercase, doesn't matter if not letter
...
24
votes
Tips for golfing in R
Some very specific golfing tips:
if you need to extract the length of a vector, sum(x|1) is shorter than length(x) as long as <...
24
votes
Tips for golfing in Rust
Use closures instead of functions
A closure:
|n|n+1
is shorter than a function:
fn f(n:i32)->i32{n+1}
Closures longer ...
24
votes
Accepted
Brainf*ck Loop Problem
27 24 bytes
++[<++[++<]>>>+]<[-<-.>]
Spent a whole day basically writing up a brute forcer and watching the results come in. Now I can ...
24
votes
Tips for creating polyglots
Exploit comment symbols
A simple way to create a two-language polyglot is to have the code divided in two parts as follows:
The first part does the actual work in language A, is harmless in language ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
tips × 370code-golf × 319
python × 61
javascript × 29
c × 16
basic × 10
math × 8
array × 8
haskell × 8
php × 7
restricted-source × 6
assembly × 6
golfscript × 6
number × 5
c++ × 5
c# × 5
lisp × 5
pyth × 5
ruby × 5
powershell × 5
kolmogorov-complexity × 4
binary × 4
java × 4
machine-code × 4
sql × 4