Hot answers tagged

96 votes

Tips for golfing in Python

Store lookup tables as magic numbers Say you want to hardcode a Boolean lookup table, like which of the first twelve English numbers contain an n. ...
user avatar
  • 138k
87 votes

Tips for golfing in Python

Collapse two numerical loops into one Say you're iterating over the cells of an m*n grid. Instead of two nested for loops, one ...
user avatar
  • 138k
75 votes

Tips for golfing in Python

For integer n, you can write n+1 as -~n n-1 as ...
user avatar
  • 138k
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 ...
user avatar
  • 207k
59 votes

Tips for golfing in Python

Extended iterable unpacking ("Starred assignment", Python 3 only) The best way to explain this is via an example: ...
user avatar
  • 61k
42 votes

Tips for golfing in Python

Choosing one of two numbers based on a condition You already know to use the list selection [x,y][b] with a Boolean b for the ...
user avatar
  • 138k
42 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 ...
user avatar
  • 138k
40 votes

Tips for golfing in Python

Use ~ to index from the back of a list If L is a list, use L[~i] to get the i'th element ...
user avatar
  • 138k
40 votes

Smallest python script to print even numbers 0 to 100

Python 2 - 12 characters print 8**999 The decimal representation of all even numbers from 0 to 100 can be found in the output: ...
user avatar
  • 30.9k
37 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 ...
user avatar
  • 16.9k
36 votes
Accepted

How can I use cmp(a,b) with Python3?

Python 3 does not have cmp. For golfing, you can do 11 chars (a>b)-(a<b) which loses 3 chars over ...
user avatar
  • 138k
33 votes

Tips for golfing in Python

PEP448 – Additional Unpacking Generalizations With the release of Python 3.5, manipulation of lists, tuples, sets and dicts just got golfier. Turning an iterable into a set/list Compare the pairs: ...
user avatar
  • 61k
33 votes

Tips for golfing in Python

Ceil and Floor If you ever want to get the rounded-up result for a division, much like you'd do with // for floor, you could use ...
user avatar
  • 1,476
31 votes

Smallest python script to print even numbers 0 to 100

Python 3, 22 (Possibly not allowed) If the challenge is "to create a python script which prints the even numbers from 0 to 100" and not "to create a python script which prints the even ...
user avatar
  • 41.4k
31 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 ...
user avatar
  • 99.8k
28 votes

Tips for golfing in Python

Exploit Python 2 string representations Python 2 lets you convert an object x to its string representation `x` at a cost of ...
user avatar
  • 138k
28 votes

Tips for golfing in Python

Length tradeoff reference I've think it would be useful to have a reference for the character count differences for some common alternative ways of doing things, so that I can know when to use which. ...
user avatar
  • 138k
26 votes

Is there a shorter way to assign one of two variables in Python?

12 chars/assignment + 9 chars of overhead V=vars() # do once at the start of the program V["xy"[b]]=z Note that this only works at global scope, it does not ...
user avatar
26 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 ...
user avatar
  • 17.9k
25 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 ...
user avatar
  • 66.1k
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 ...
23 votes

Tips for golfing in Python

Arithmetic tricks Here are some arithmetic tricks which are either shorter or are more useful due to precedence rules. ...
user avatar
  • 61k
23 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 ...
user avatar
  • 61k
23 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 ...
user avatar
  • 138k
23 votes

Smallest python script to print even numbers 0 to 100

Python2 26 i=0;exec"print i;i+=2;"*51 independent discovery of @bitpwner's solution
user avatar
  • 5,988
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 Python

The shortest infinite for comprehension You may know the trick to easily create an infinite generator using the two-argument form of iter: ...
user avatar
  • 22.2k
21 votes
Accepted

Golf Practice: Python

Total: 104 101 99 chars Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Problem 6 Problem 7 Problem 8 Problem 9 Problem 10
user avatar
  • 19k
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. ...
user avatar
  • 36.1k
21 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 ...
user avatar

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