Skip to main content
Share Your Experience: Take the 2024 Developer Survey
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 ...
Dennis's user avatar
  • 210k
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 ...
Laikoni's user avatar
  • 26.2k
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) ...
ceilingcat's user avatar
  • 7,605
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 ...
xnor's user avatar
  • 145k
42 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 ...
Kevin Cruijssen's user avatar
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 ...
Luis Mendo's user avatar
  • 105k
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 ...
Luis Mendo's user avatar
  • 105k
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 ...
dingledooper's user avatar
  • 22.7k
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 ...
xnor's user avatar
  • 145k
28 votes

Tips for golfing in JavaScript

Use ^ instead of != or == when comparing to an integer ...
Tomas Langkaas's user avatar
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 ...
lynn's user avatar
  • 68.7k
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 <...
JDL's user avatar
  • 1,733
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 ...
Wheat Wizard's user avatar
  • 98.8k
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: ...
pxeger's user avatar
  • 23.8k
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 ...
l4m2's user avatar
  • 24.4k
25 votes

Tips for golfing in <all languages>

use - instead of != for numeric comparisons: If a equals b, a-b results in ...
Titus's user avatar
  • 14.7k
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: ...
Martin Ender's user avatar
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 ...
Arnauld's user avatar
  • 193k
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 ...
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 ...
dingledooper's user avatar
  • 22.7k
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 ...
Sp3000's user avatar
  • 61.8k
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 ...
Luis Mendo's user avatar
  • 105k
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 ...
feersum's user avatar
  • 31.4k
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 ...
xnor's user avatar
  • 145k
22 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 ...
Kevin Cruijssen's user avatar
22 votes

Tips for golfing in Haskell

Match a constant value A list comprehension can pattern match on a constant. [0|0<-l] This extracts the 0's of a list l, i....
xnor's user avatar
  • 145k
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. ...
Peter Cordes's user avatar
  • 4,837
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 ...
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 ...
DELETE_ME's user avatar
  • 17.3k

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