This challenge is related to the Python language. Note that challenges that require the answers to be in a specific language are generally discouraged.
160
votes
111answers
38k views
Tips for golfing in Python
What general tips do you have for golfing in Python? I'm looking for ideas which can be applied to code-golf problems and which are also at least somewhat specific to Python (e.g. "remove comments" is ...
18
votes
7answers
1k views
How do I alias member functions in Python?
In Python, one can save bytes by aliasing functions that are used repeatedly. For example:
r=range
a=r(100)
b=r(200)
c=r(300)
However, when the functions are member functions together, I don't know ...
11
votes
13answers
30k views
Smallest python script to print even numbers 0 to 100
I'm work on a problem which I set myself for fun, which is to create a python script which prints the even numbers from 0 to 100. The challenge is to make the script as small as possible. This is what ...
11
votes
1answer
1k views
What is the minimum set of characters with which you can write any valid python program?
The proof of the set of characters is that there is a translator which takes any ascii python program as input and produces a limited character set version of the program. The limited character set ...
5
votes
8answers
610 views
Golfing the Core
Note: although this is tagged as Python, other languages are permitted
Challenge
What you have to do is write the shortest functions to perform the same actions as the following Python 2 built in ...
28
votes
7answers
6k views
Golf Practice: Python [closed]
This is a challenge of practicing golf optimization in Python -- reusable tricks and shortcuts to shave off a few characters. Many will be familiar to Python golfers and use common ideas from the ...
4
votes
3answers
1k views
Solve the CodeSprint4 Leibniz code golf challenge in Python in 66 characters
Check out this challenge:
https://www.hackerrank.com/codesprint4/challenges/leibniz
(The competition has already ended, so I'm just curious about the Python solution, which the organizers refused to ...
4
votes
1answer
2k views
Shortest Way of creating a basic Space Invaders Game in Python
Hello all code golfers:
Your challenge is to create a basic version of Space Invaders in Python
Rules
Program must be not written in Py3K
All levels must be randomly generated
There must be a HUD ...
12
votes
3answers
524 views
Auto-meta-code-golf
You are sick of all of the codegolf challenges. Hence you decide to write a program that will automatically golf some Python code for you. There are 3 test cases:
print quickSort([0,7,3,-1,8,10,57,...
9
votes
1answer
394 views
How to find the item in a list whose f(item) is the smallest?
I have a list, l and a function f. f is not strictly increasing or decreasing. How can I find the item in the list whose f(item) is the smallest? For example, let's say the list is:
l = [1, 2, 3, 4]
...
13
votes
1answer
684 views
Golfing with Python Import
When is it beneficial to use inline, single use importing in Python?
For example:
__import__("x").doSomething()
Is the above ever shorter than the below?
import x
x.doSomething()
Or
from x ...
12
votes
1answer
221 views
Python advice: Portability of introspective function call
In Python, you can use the dir function on any object to get a list of the names of its instance functions:
>>> dir('abc')
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '...
8
votes
1answer
1k views
Palindromic prime
I'm solving task, where:
Input:
A number as an integer.
Output:
The closest greater palindromic prime as an integer.
I would appreciate hints how to make my solution shorter. Or directions if ...
7
votes
3answers
342 views
How can I shorten this python code analyzing a 3d grid?
My Python 3 function golf(...) should take a list of lists of lists of strings representing a solid cube and return whether there are any places in which two equal strings are directly next to each ...
5
votes
4answers
576 views
How could I reduce the length of this code?
I need to reduce the length of this code in Python 3 as much as possible (even if it will be less readable):
a,b,x,y=[int(i) for i in input().split()]
while 1:
r=''
if y<b:r='S';y+=1
if y>b:...