Skip to main content

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
341 votes
184 answers
155k views

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
  • 13k
88 votes
11 answers
17k views

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
  • 150k
59 votes
27 answers
9k views

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 ...
Gryphon's user avatar
  • 7,315
58 votes
49 answers
12k views

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 ...
maxb's user avatar
  • 7,017
55 votes
37 answers
11k views

Final Standings +----------------------------------+---------+---------+---------+----------------------------+ | Name | Score | WinRate | TieRate | Elimination ...
KBriggs's user avatar
  • 691
43 votes
17 answers
7k views

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
  • 103k
38 votes
24 answers
5k views

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. ...
izzyg's user avatar
  • 42.3k
38 votes
3 answers
6k views

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
  • 150k
38 votes
3 answers
2k views

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 coinheringaahing's user avatar
37 votes
4 answers
3k views

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 ...
Esolanging Fruit's user avatar
33 votes
6 answers
18k views

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
  • 150k
32 votes
34 answers
6k views

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 ...
Colera Su's user avatar
  • 2,431
32 votes
7 answers
9k views

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
  • 150k
27 votes
7 answers
12k views

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,396
27 votes
14 answers
3k views

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 ...
mousetail's user avatar
  • 14.4k
27 votes
13 answers
2k views

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 ...
Beefster's user avatar
  • 10k
22 votes
9 answers
4k views

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 ...
Have a nice day's user avatar
22 votes
1 answer
731 views

Background Python 3 has many types of string literals. For example, the string this 'is' an exa\\m/ple can be represented as: ...
PurkkaKoodari's user avatar
20 votes
19 answers
5k views

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 ...
SIGSTACKFAULT's user avatar
20 votes
1 answer
805 views

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
  • 23.4k
19 votes
1 answer
45k views

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. ...
xaxa's user avatar
  • 261
19 votes
4 answers
7k views

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: ...
DJMcMayhem's user avatar
  • 60.1k
19 votes
2 answers
1k views

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 ...
Wheat Wizard's user avatar
  • 103k
18 votes
3 answers
3k views

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 ...
xnor's user avatar
  • 150k
18 votes
16 answers
3k views

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 ...
izzyg's user avatar
  • 42.3k
17 votes
10 answers
9k views

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 ...
George's user avatar
  • 1,367
17 votes
24 answers
3k views

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,315
17 votes
19 answers
6k views

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 ...
mousetail's user avatar
  • 14.4k
17 votes
4 answers
2k views

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: ...
user82938's user avatar
  • 181
17 votes
5 answers
2k views

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” ...
izzyg's user avatar
  • 42.3k
17 votes
3 answers
3k views

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.6k
17 votes
2 answers
1k views

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 ...
wim's user avatar
  • 903
16 votes
17 answers
1k views

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: ...
Miriam's user avatar
  • 843
16 votes
8 answers
1k views

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... ...
dingledooper's user avatar
  • 23.4k
16 votes
4 answers
1k views

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
  • 658
15 votes
12 answers
1k views

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 ...
AlienAtSystem's user avatar
14 votes
16 answers
3k views

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 ...
Qaziquza's user avatar
  • 1,693
14 votes
7 answers
1k views

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 ...
user avatar
14 votes
1 answer
9k views

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 ...
DLosc's user avatar
  • 40.7k
14 votes
2 answers
2k views

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 ...
thouis's user avatar
  • 93
14 votes
1 answer
330 views

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.8k
13 votes
5 answers
1k views

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 ...
Leon's user avatar
  • 327
13 votes
4 answers
725 views

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
13 votes
1 answer
2k views

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
13 votes
1 answer
1k views

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: ...
motoku's user avatar
  • 241
13 votes
3 answers
956 views

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
13 votes
1 answer
780 views

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 ...
Neil's user avatar
  • 2,535
12 votes
13 answers
56k views

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
12 votes
19 answers
2k views

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
16 answers
2k views

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: ...
Spitemaster's user avatar
  • 2,189