Questions tagged [python]
This challenge is related to the Python language. Note that challenges that require the answers to be in a specific language are generally discouraged.
37
questions
330
votes
179
answers
142k
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 ...
42
votes
17
answers
6k
views
Tips for restricted source in Python
Just like code-golf, restricted-source pushes one to exploit quirks and hidden features of the Python language. We already have a place to collect all these tips for code-golf, those for restricted-...
86
votes
11
answers
16k
views
How on earth did llhuii output the Evil Numbers in 42 bytes of Python?
This is a tips question for golfing in Python concerning the Evil Numbers question on Anarchy Golf.
An number is evil if its binary expansion has an even number of 1's. The challenge is to print the ...
38
votes
3
answers
5k
views
Primality testing formula
Your goal is to determine whether a given number n is prime in the fewest bytes. But, your code must be a single Python 2 expression on numbers consisting of only
...
27
votes
7
answers
12k
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 ...
5
votes
9
answers
908
views
Golfing the Core [closed]
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 ...
32
votes
7
answers
9k
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 ...
17
votes
3
answers
3k
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?
...
15
votes
4
answers
947
views
Tips for golfing with numpy, scipy, or pylab
There's already a comprehensive list of tips for python here, so what I'm asking for are tips that specifically apply to using the numpy, ...
5
votes
1
answer
349
views
all non-empty sublists partitionings of a list
I had to write a code for finding all non-empty sublists partitionings of a list:
...
33
votes
6
answers
16k
views
Python workarounds for assignment in lambda
This is a tips question for golfing in Python.
In Python golfing, it's common for a submission to be a function defined as a lambda. For example,
...
12
votes
1
answer
2k
views
What is the minimum set of characters with which you can write any valid python program? [duplicate]
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 ...
11
votes
13
answers
55k
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 ...
10
votes
1
answer
666
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 ...
7
votes
3
answers
541
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 ...
4
votes
3
answers
2k
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 ...
55
votes
37
answers
10k
views
Robot Roulette: High stakes robot gambling
Final Standings
+----------------------------------+---------+---------+---------+----------------------------+
| Name | Score | WinRate | TieRate | Elimination ...
38
votes
3
answers
1k
views
Build a golfing language with me
A lot of PPCG users helped with the creation of this challenge, both in chat and the Sandbox, specifically Martin Ender, AdmBorkBork, Emigna and user202729
Our community has found it necessary to ...
20
votes
1
answer
647
views
How to solve the LCM in 50 bytes of Python
I've recently stumbled upon a Russian site called acmp.ru, in which one of the tasks, HOK, asks us to find the LCM of two positive integers. The full statement, translated to English is as follows:
...
17
votes
24
answers
2k
views
Prisoner's Dilemma v.3 - Petri Dilemma
A mad scientist has just created a new species of bacterium! He has decided to name it Noblus Gentlemanus, after observing its behaviour. However, his bacteria have ran out of food, and have ...
14
votes
1
answer
298
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:
...
13
votes
3
answers
848
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:
...
12
votes
2
answers
3k
views
How can I shorten this python code?
Here is the code which I want to shorten.
...
12
votes
3
answers
1k
views
I'm Jelly of Python (Cops)
What does "jelly" mean in the title?. Robber's thread
Jelly has an "Evaluate the argument as Python 3" atom, ŒV which takes a string as an ...
12
votes
19
answers
2k
views
KOTH - Loaded RPS
Contest permanently opened - Updated August 10th 2017
Even though on June 5th 2017 I declared a winner (who will be kept as the best answer) I'll be rnning new bots and updating the results.
June 5th ...
12
votes
4
answers
634
views
Strategies for representing a given large integer using arithmetic expression
I have a specific number in mind, but it's part of a challenge I'm doing, and I don't want people to do (all) the work for me.
Here is a number which has the same digits, but shuffled:
...
9
votes
4
answers
3k
views
How to shorten the python code?
I'm writing a program that takes a number from input (call it x), loops from 0 to (x - 1) (let's call our iterator y) and prints \$y^x+x\$ for each y.
After the loop, it then prints "SUM: N" ...
8
votes
1
answer
389
views
I'm Jelly of Python (Robbers)
What does "jelly" mean in the title?. Cop's thread
Robbers, you are to choose an uncracked Cop answer, and attempt to find a Jelly program which is:
shorter than the Cop's answer
solves the ...
8
votes
1
answer
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 change ...
5
votes
4
answers
3k
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):
...
5
votes
3
answers
2k
views
A nasty little memory leak
Below Python code generates memory leak.
Challenge: Fix the memory leak with minimal changes to the code as measured by Levenshtein distance to the original code. You may only change the code before ...
4
votes
1
answer
3k
views
Shortest Way of creating a basic Space Invaders Game in Python
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 showing the players score
...
4
votes
2
answers
1k
views
Write an infinitely nested generator comprehension
The goal is to create a generator generator [...] generator generator comprehension.
The generator must be created via generator comprehension.
The following expression must be valid for any number ...
3
votes
2
answers
1k
views
Shortening str.replace() [duplicate]
In Python it is often necessary to use str.replace(a,b), however .replace is rather long and feels unnecessary. So is there a ...
2
votes
4
answers
1k
views
secret language hackerrank.com
I tried this contest: https://www.hackerrank.com/challenges/secret-language
Well, the score in this contest said I made 30 out of 30 points. As the score is calculated with the formula 30*(1000-#chars)...
0
votes
7
answers
657
views
Shortest way to get a TypeError
It's simple, simply write the shortest code to raise a TypeError.
In Python you can do:
raise TypeError
However it should be ...
-4
votes
1
answer
240
views
Python Prime Problem [duplicate]
Your challenge is to write a Python program to print all the primes (separated by whitespace) less than a given integer N with an asterisk (...