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 ...
47
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 ...
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 to ...
33
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 ...
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 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 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
23
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 ...
23
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 ...
23
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
23
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 ...
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.
...
18
votes
Tips for restricted source in Python
Use << and | to generate constant without +
Fun fact: You can get any positive ...
18
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 ...
17
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.
...
17
votes
Get the lowest possible pylint score within 128 bytes of python
-5430
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++exit("Hello World")
Try it online!
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 ...
17
votes
Accepted
17
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 ...
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 to ...
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 ...
15
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 ...
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
...
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, ...
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
...
14
votes
Tips for restricted source in Python
Use ord() or binary strings to avoid digits
Most integers in the ranges [32..47] and ...
Community wiki
13
votes
How on earth did llhuii output the Evil Numbers in 42 bytes of Python?
llhuii’s Python 3 submission
Here are the Python 3 submissions for Evil Numbers at the time of writing:
llhuii probably ported their trick to Python 3, and came up with a solution that is
3 bytes ...
13
votes
Honest Rock, Paper, Scissors
Mason
Tries to pick up information about other bots such as how honest they are and how they're effected by my first move. I then try and find other obvious bots that follow a pattern and exploit ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
python × 144code-golf × 87
tips × 62
king-of-the-hill × 27
game × 9
math × 7
string × 6
code-challenge × 5
optimization × 5
programming-puzzle × 5
popularity-contest × 4
fastest-code × 4
array × 3
decision-problem × 3
primes × 3
restricted-source × 3
palindrome × 3
card-games × 3
board-game × 3
number × 2
kolmogorov-complexity × 2
number-theory × 2
combinatorics × 2
geometry × 2
cops-and-robbers × 2