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.

Filter by
Sorted by
Tagged with
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 ...
moinudin's user avatar
  • 12.7k
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-...
Wheat Wizard's user avatar
  • 98.5k
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 ...
xnor's user avatar
  • 145k
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 ...
xnor's user avatar
  • 145k
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 ...
Rainbolt's user avatar
  • 6,376
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 ...
Beta Decay's user avatar
  • 23.4k
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 ...
xnor's user avatar
  • 145k
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? ...
Beta Decay's user avatar
  • 23.4k
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, ...
user2699's user avatar
  • 648
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: ...
fferri's user avatar
  • 163
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, ...
xnor's user avatar
  • 145k
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 ...
user avatar
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 ...
Benedict Lewis's user avatar
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 ...
DJMcMayhem's user avatar
  • 59.2k
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 ...
Byte Commander's user avatar
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 ...
Ram Rachum's user avatar
55 votes
37 answers
10k views

Robot Roulette: High stakes robot gambling

Final Standings +----------------------------------+---------+---------+---------+----------------------------+ | Name | Score | WinRate | TieRate | Elimination ...
KBriggs's user avatar
  • 691
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 ...
caird coinheringaahin g's user avatar
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: ...
dingledooper's user avatar
  • 22.7k
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 ...
Gryphon's user avatar
  • 7,235
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: ...
feersum's user avatar
  • 31.4k
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: ...
Nathan Merrill's user avatar
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: ...
Christian Sonne's user avatar
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 ...
Masclins's user avatar
  • 943
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 ...
caird coinheringaahin g's user avatar
12 votes
2 answers
3k views

How can I shorten this python code?

Here is the code which I want to shorten. ...
Ranvijay Singh's user avatar
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" ...
Fmbalbuena's user avatar
  • 3,847
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 ...
caird coinheringaahin g's user avatar
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 ...
Martin Rajnoha's user avatar
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): ...
xaxa's user avatar
  • 231
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 ...
Shital Shah's user avatar
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 ...
Amith KK's user avatar
  • 215
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 ...
DivideByZero's user avatar
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 ...
Beta Decay's user avatar
  • 23.4k
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)...
Johannes Maria Frank's user avatar
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 ...
U13-Forward's user avatar
  • 2,001
-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 (...
user101295's user avatar