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.
143 questions
341
votes
184
answers
155k
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 ...
88
votes
11
answers
17k
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 ...
59
votes
27
answers
9k
views
Honest Rock, Paper, Scissors
Many people consider RPS to be a game of chance. If both players play unpredictably, the best strategy is to play randomly. However, let's introduce a bit of predictability to it.
Each bot will have ...
58
votes
49
answers
12k
views
A game of dice, but avoid number 6
Tournament over!
The tournament is now over! The final simulation was run during the night, a total of \$3*10^8\$ games. The winner is Christian Sievers with his bot OptFor2X. Christian Sievers also ...
55
votes
37
answers
11k
views
Robot Roulette: High stakes robot gambling
Final Standings
+----------------------------------+---------+---------+---------+----------------------------+
| Name | Score | WinRate | TieRate | Elimination ...
43
votes
17
answers
7k
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-...
38
votes
24
answers
5k
views
Noisy Iterated Prisoner's Dilemma
In this challenge, you will play the noisy iterated prisoner's dilemma.
The Prisoner's dilemma is a scenario in game theory where there are two players, each with two options: cooperate, or defect. ...
38
votes
3
answers
6k
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
...
38
votes
3
answers
2k
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 ...
37
votes
4
answers
3k
views
FOIL Python's strong typing!
Your task is to write some code in Python 2 or 3 such that this expression:
(a+b)(c+d) == a*c + b*c + a*d + b*d
will evaluate to ...
33
votes
6
answers
18k
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,
...
32
votes
34
answers
6k
views
First-price sealed-bid auction
Final Result
The competition is over. Congratulations to hard_coded!
Some interesting facts:
In 31600 out of 40920 auctions (77.2%), the winner of the first round ...
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 ...
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 ...
27
votes
14
answers
3k
views
Is this series of quotes valid Python?
Python string parsing has quite a few edge cases. This is a string:
"a"
Putting 2 strings immediately after each other implicitly concatenates them, so ...
27
votes
13
answers
2k
views
Adventurers in the Ruins
Test Driver • Challenge Discussion • Submit Adventurer
(Image Source)
Several rival adventurers are raiding the ruins for treasure, but they can only carry so much at a time and have their ...
22
votes
9
answers
4k
views
How can this code be more "golfed"?
I'm not sure if this is the right place to ask, but I found this similar question so I'll go ahead.
I'm very new to code golfing, so keep your smothered laughs and throw-up emoji's to yourselves ...
22
votes
1
answer
731
views
Golfing Python string literals
Background
Python 3 has many types of string literals. For example, the string this 'is' an exa\\m/ple can be represented as:
...
20
votes
19
answers
5k
views
Iterated Prisoner's Trilemma
CHALLENGE STATUS: OPEN
Comment, open a PR, or otherwise yell at me if I'm missing your bot.
Prisoner's dilemma ... with three choices. Crazy, huh?
Here's our payoff matrix. Player A on the left, B ...
20
votes
1
answer
805
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:
...
19
votes
1
answer
45k
views
How can I use cmp(a,b) with Python3?
I was told that using the cmp function can be very useful in code-golf. But unfortunately, Python 3 doesn't have a cmp function.
...
19
votes
4
answers
7k
views
Is there a shorter way to get user input in python?
This is not a challenge. I'm wondering if it's at all possible to get user input into two separate variables in python (2 or 3) with less than 19 bytes. These are all the shortest I can get:
...
19
votes
2
answers
1k
views
Are these lists equal?
As you may very well know python has lists. As you might not know these lists can contain themselves.
a = []
a.append(a)
Python 2
Python 3
These are cool and ...
18
votes
3
answers
3k
views
Is there a shorter way to assign one of two variables in Python?
This is a tips question for golfing in python.
In multiple golfs I've done in Python, a fixed value is assigned to one of two variables chosen by a Boolean. The chosen variable is overwritten by the ...
18
votes
16
answers
3k
views
Adversarial Iterated Prisoner's Dilemma
In this challenge, you will play the iterated Prisoner's Dilemma, but with a twist: There's also an adversary trying to mess you up!
The Prisoner's dilemma is a scenario in game theory where there are ...
17
votes
10
answers
9k
views
Crash (i.e. cause the interpreter to stop working and force close) Python [closed]
I would like to see who can make Python have a fatal crash with the most creative code. This means that when the program runs, Windows for example, will take over and pop up with something like “IDLE ...
17
votes
24
answers
3k
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 ...
17
votes
19
answers
6k
views
The Dating Game / Secretary Problem
The bots are looking for love. Can you help them?
The rules
The goal of this game is find the bot you have the highest compatibility with. However, robots, who are inexperienced at dating are unable ...
17
votes
4
answers
2k
views
Get the lowest possible pylint score within 128 bytes of python
pylint has no lower bound on the scores it will give your code. It uses the following metric to score code, with the maximum score being 10:
...
17
votes
5
answers
2k
views
Abstract Syntax Tree Golfing: FizzBuzz, Python
Summary
Implement FizzBuzz in Python, with the fewest possible tokens.
Challenge
Write a program that prints the decimal numbers from 1 to 100 inclusive. But for multiples of three print “Fizz” ...
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?
...
17
votes
2
answers
1k
views
Maximum number of PEP8 violations in a single line
Your task is to write a file which contains a line with many pep8 violations.
The rules:
We use pep8 version 1.5.7 and the default settings.
Calling pep8 with other command line options or using ...
16
votes
17
answers
1k
views
Liars and Guessers
Github
You have probably played, and may even have written a program to play, a simple number guessing game. If so, this probably looks familiar:
...
16
votes
8
answers
1k
views
Omit needless spaces for me
A great Python golfing tip is to omit needless spaces. But if you are like me, you don't always have the time to search for which spaces to remove. If only there was a way to speed up the process...
...
16
votes
4
answers
1k
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, ...
15
votes
12
answers
1k
views
The Celestial Bureaucracy KoTH
In Imperial China, ranks in society were not decided by birth or wealth, but by a person's ability to excel in the Imperial Examinations. The Jade Emperor, divine ruler of the Heavens, has called for ...
14
votes
16
answers
3k
views
Speed up, slowpoke!
In this king-of-the-hill challenge, you will program a bot, consisting of a python function of the form
def BOTNAME(info, me, round):
pass # ... main body
...
14
votes
7
answers
1k
views
Chopsticks mutated with points KoTH
Basic rules (different from mine)
Story
It is the year 4579, humans now have 2 hands with 1001 fingers each. Chopsticks has become based off of points. And @Dennis has more rep then @Martin... Hand ...
14
votes
1
answer
9k
views
Concatenate lists of strings item-wise in Python
This is a tips question for golfing in python.
Suppose you have two lists of strings, and you want to concatenate the corresponding entries from each list. E.g. with ...
14
votes
2
answers
2k
views
Fastest python code to find a set of winning words in this game
This is a word game from a set of activity cards for children. Below the rules is code to find the best triplet using /usr/share/dict/words. I thought it was an interesting optimization problem, and ...
14
votes
1
answer
330
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
5
answers
1k
views
Add comments to a Python script and make it a bilingual Python/C++ “program”
Given the following Python 3 script:
def greet():
print("Hello, world!")
greet()
Prepend some lines to this text file so that it can be both executed as a ...
13
votes
4
answers
725
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:
...
13
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 ...
13
votes
1
answer
1k
views
Am I code-golfing properly?
I am curious if I am Code Golfing properly. I set the challenge for myself to make a small hashing program into a single statement in Python. I first started off with:
...
13
votes
3
answers
956
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:
...
13
votes
1
answer
780
views
Let's write a Minifier
Background
Minifiers are used, commonly, when serving JavaScript to your Web Browser. It is commonly used to reduce the number of bytes that have to be sent. Saving bandwidth is useful for obvious ...
12
votes
13
answers
56k
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 ...
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
16
answers
2k
views
Random Prisoner's Trilemma - Python 3 KOTH
The Prisoner's Dilemma, but with 3 choices, and the payoffs are random!
Each round, your bot recieves a 3x3 grid and chooses a row to play. The grid might be this:
...