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
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
  • 145k
39 votes

Primality testing formula

43 bytes (4**n+1)**n%4**n**2/n&2**(2*n*n+n)/-~2**n<1 Try it online! The method is similar to Dennis' second (deleted) answer, but this answer is easier ...
DELETE_ME's user avatar
  • 17.3k
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.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
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
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.7k
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 ...
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

A game of dice, but avoid number 6

NeoBot Instead, only try to realize the truth - there is no spoon NeoBot peeks into the matrix (aka random) and predicts if the next roll will be a 6 or not - it can't do anything about being handed ...
Mostly Harmless's user avatar
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
Accepted

FOIL Python's strong typing!

54 52 50 49 48 45 39 bytes Removed 4 bytes thanks to Dennis. The latest version is inspired by the "some reason" in xnor's answer. ...
jimmy23013's user avatar
20 votes

Tips for restricted source in Python

Function calls without parentheses We can avoid using parentheses for operator precedence using list indexing, but parentheses are still very useful for calling functions. List indexing can be used ...
Wheat Wizard's user avatar
  • 98.5k
18 votes
Accepted

How can I shorten this python code?

Instead of ((a*10+c)*10)+d we can use ((a*2+c)*2)+d to distinguish the colors. ...
Lynn's user avatar
  • 68.7k
18 votes

Tips for restricted source in Python

Use << and | to generate constant without + Fun fact: You can get any positive ...
TwilightSparkle's user avatar
17 votes

Tips for golfing in Python

Reading multi-line input In Python 3, the built-in function open underwent some changes. In particular, its first argument file is either a string or bytes ...
Dennis's user avatar
  • 210k
17 votes

Get the lowest possible pylint score within 128 bytes of python

-5430 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++exit("Hello World") Try it online!
Bryan K's user avatar
  • 271
17 votes

Robot Roulette: High stakes robot gambling

UpYours Being late to enter I spent a while admiring the existing bots, spent a while overcomplicating your guys' ideas, then un-overcomplicating them. Then it came to me Good artists copy, great ...
Qfwfq's user avatar
  • 371
16 votes

Tips for golfing in Python

Split into chunks You can split a list into chunks of a given size using zip and iter, as explained in this SO question. ...
xnor's user avatar
  • 145k
16 votes
Accepted

Shortest way to get a TypeError

...
tsh's user avatar
  • 34.7k
16 votes

Tips for restricted source in Python

Access methods and built-in functions through __dict__ Classes contain a __dict__ attribute, which maps their method names to ...
water_ghosts's user avatar
  • 1,596
16 votes
Accepted

How to ignore first user input in Python 3

open(0), 40 bytes The builtin function open returns a file object which can be iterated over, line by line. Usually you pass a ...
ovs's user avatar
  • 59.3k
15 votes

Robot Roulette: High stakes robot gambling

Kamikaze Why bother with complicated logic when we are all going to die anyway... def kamikaze(hp, history, ties, alive): return hp One shot It's going ...
DobromirM's user avatar
  • 323
15 votes

A game of dice, but avoid number 6

Cooperative Swarm Strategy I don't think anyone else has yet noticed the significance of this rule: If the game goes to 200 rounds, the bot (or bots) with the highest score is the winner, even if ...
Einhaender's user avatar
15 votes
Accepted

Shortest Solution in Python 3 for Caught Speeding - CodingBat

Python 3, 32 bytes lambda a,b:(a-b*5>60)+(a-b*5>80) Try it online! Python 3, 50 49 bytes If io must be done with stdin and stdout ...
Mukundan314's user avatar
  • 10.6k
14 votes

Tips for golfing in Python

If you want to know the type of a variable x: ...
G B's user avatar
  • 21.9k
14 votes

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

This gives my (xnor's) 47-byte solution and the thinking that led me to it. Don't read this if you want to figure this out yourself. A natural first idea is to iterate through the numbers 0 to 799, ...
xnor's user avatar
  • 145k
14 votes

Robot Roulette: High stakes robot gambling

Pathetic Bot gets a much needed upgrade: The pathetic attempt at a bot that tries to incorporate other bots' features ...
Yodie's user avatar
  • 141
14 votes
Accepted

Convert a string containing integers to list of integers

Using map is way shorter than any of the ways I mentioned. You should do that. Instead of calling list(...), you should use ...
DJMcMayhem's user avatar
  • 59.2k

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