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 ...
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 ...
47
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)
...
46
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 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 ...
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 ...
30
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 ...
27
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 <...
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
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
Abuse literals
The recent sample: Check whether "c" is uppercase or lowercase, doesn't matter if not letter
...
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
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 ...
23
votes
Tips for golfing in Java
Some small code-golfing tips
These tips were a bit too small for a separated answers, so I will use this answer for very small codegolfing tips that I found or came up with, and aren't mentioned in ...
23
votes
Tips for Golfing in Brain-Flak
Finding modulus/remainder
Finding n modulo m is one of the basic arithmetic operations, important for many challenges. For cases m > 0 and n >= 0, the following 46-byte snippet may be used. It ...
22
votes
Tips for golfing in Python
Binomial coefficient
The binomial coefficient \$\binom{n}{k} \ = \frac{n!}{k!(n-k)!}\$ can be expressed arithmetically as
((2**n+1)**n>>n*k)%2**n
Try it ...
22
votes
Tips for golfing in x86/x64 machine code
Choose your calling convention to put args where you want them.
The language of your answer is asm (actually machine code), so treat it as part of a program written in asm, not C-compiled-for-x86. ...
22
votes
Tips for restricted source in Python
Avoid Parens with List indexing
Parentheses are super useful for creating the correct operator precedence so it is a bummer when they are banned. However if [] are ...
Community wiki
21
votes
Tips for golfing in C++
Shorter header
This is GCC specific, it may be extensible to other compilers.
Precompiled header.
In G++ bits/stdc++.h is the precompiled header consists of all ...
21
votes
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
tips × 378code-golf × 326
python × 62
javascript × 30
c × 16
basic × 10
math × 8
array × 8
haskell × 8
golfscript × 8
php × 7
restricted-source × 6
assembly × 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