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
291
votes
153answers
101k 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 ...
38
votes
16answers
5k 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-...
80
votes
11answers
14k 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 ...
25
votes
7answers
9k 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
9answers
879 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
7answers
8k 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 ...
16
votes
3answers
2k 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? ...
5
votes
1answer
300 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: ...
35
votes
3answers
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 ...
12
votes
1answer
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 ...
33
votes
6answers
8k 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, ...
11
votes
13answers
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
1answer
651 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 ...
4
votes
3answers
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 ...
11
votes
2answers
358 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, ...
7
votes
3answers
484 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 ...
55
votes
37answers
10k views

Robot Roulette: High stakes robot gambling

Final Standings +----------------------------------+---------+---------+---------+----------------------------+ | Name | Score | WinRate | TieRate | Elimination ...
33
votes
3answers
927 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 ...
17
votes
24answers
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 ...
5
votes
1answer
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 ...
12
votes
4answers
601 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: ...
12
votes
19answers
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 ...
-3
votes
7answers
597 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 ...
14
votes
1answer
283 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: ...
12
votes
3answers
734 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: ...
10
votes
3answers
957 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 ...
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 change ...
8
votes
1answer
216 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 ...
5
votes
2answers
1k 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 ...
5
votes
4answers
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): ...
2
votes
4answers
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)...
-3
votes
1answer
135 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 (...