Hot answers tagged

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
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 ...
TheNumberOne's user avatar
  • 11.5k
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.1k
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 ...
xnor's user avatar
  • 144k
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) ...
ceilingcat's user avatar
  • 7,467
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 ...
Lynn's user avatar
  • 68.1k
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 ...
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
  • 104k
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 ...
Lynn's user avatar
  • 68.1k
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
  • 104k
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.3k
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
  • 144k
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.1k
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 (...
Yytsi's user avatar
  • 3,762
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
  • 96.6k
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 ...
Sp3000's user avatar
  • 61.7k
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.3k
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 <...
George Reith's user avatar
  • 2,554
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.6k
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
  • 189k
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.3k
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 ...
l4m2's user avatar
  • 20.7k
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 <...
JDL's user avatar
  • 1,673
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 ...
Doorknob's user avatar
  • 71.4k
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.7k
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
  • 104k

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