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
15
votes
12answers
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 ...
5
votes
2answers
418 views

Farkle Fest 2k19: An AI dice game tournament

Farkle Fest 2k19 In this challenge, we will be playing the semi-popular dice game Farkle. Farkle is a dice game that has players selecting to save dice or risk rerolling the die for more points to ...
-4
votes
5answers
345 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 ...
7
votes
1answer
264 views

Convert a string containing integers to list of integers

What is the shortest way to convert a string containing integers separated by spaces to a list of integers? input : "-3 1 5 8 10" output : [-3, 1, 5, 8, 10] I regularly face this situation and I ...
28
votes
13answers
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 ...
6
votes
2answers
546 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 ...
0
votes
1answer
224 views

Python code to produce C++14 hex integer literal with ticks

tl;dr? See bottom for The Competition Integer literals While programming, the 64-bit hexadecimal literal 0x123456789abcdefL is difficult to gauge the size of. I ...
58
votes
49answers
10k views

A game of dice, but avoid number 6 [closed]

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 ...
19
votes
19answers
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 ...
56
votes
37answers
10k views

Robot Roulette: High stakes robot gambling

Final Standings +----------------------------------+---------+---------+---------+----------------------------+ | Name | Score | WinRate | TieRate | Elimination ...
5
votes
4answers
591 views

Russian BS - Submissions accepted until October 13th

The ChatRoom is here I love playing the card game BS, but sometimes, it just doesn't have enough oomph for my short attention span. Instead, let's play some Russian BS. Rules Here is a quick guide ...
12
votes
4answers
783 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: ...
9
votes
3answers
588 views

Partially-Observable Connect-4

The Game You will be playing an (almost) standard game of Connect-4. Unfortunately, it is a correspondence game and someone has placed black tape on every second row starting from the bottom, so that ...
1
vote
1answer
443 views

Dictionary Code Golf [closed]

New to code golf and enjoying it. I have a problem that I cannot figure out and am curious if others can solve it. Start with two dictionaries, y and ...
30
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 ...
-3
votes
1answer
164 views

make this bubble sort python code shorter than this? [closed]

i don't know if it can be made shorter than this ...
9
votes
2answers
2k views

Python: shortest way to interleave items from two lists

I'm trying to get the shortest way (character possible) to obtain List 3. List 1 and List 2 are already given to me as arguments and are the same length. ...
12
votes
9answers
954 views

White Elephant Exchange

It's Christmas in July, so what better way to celebrate than a virtual white elephant gift exchange! For this King of the Hill challenge, you must create a bot that plays in a White Elephant exchange ...
4
votes
5answers
1k views

Reducing a Python Script [closed]

Does anyone have any suggestions as to how I could shorten this program? ...
6
votes
4answers
693 views

Write a Program for winning sevens game

Build a function in python that can win a Sevens game provided that there are only 2 players Here are the instructions from Wikipedia: All cards are dealt to the players, even if as a result some ...
11
votes
2answers
603 views

Camel Up Cup: An AI board game tournament

Camel Up Cup 2k18 In this challenge, we will be playing the semi-popular board game Camel Up. Camel Up! is a board game that has players betting on camels to win rounds, win the game or lose the ...
-3
votes
2answers
226 views

Zero sum in python 2.7 [closed]

Write a function in Python 2.7 that takes a list of integers as an argument. It should return True or False depending on whether there are any 2 integers in the argument list that sum to zero. [4,-4,9,...
35
votes
23answers
4k 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. ...
31
votes
3answers
671 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 ...
10
votes
2answers
273 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, ...
-4
votes
1answer
580 views

Number of leap years between two given years (inclusive) [closed]

Is there a better way to calculate the number of leap years between two given years? I want to make my code shorter (in Python). Do you have any suggestions? Here is my solution: ...
14
votes
1answer
498 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 ...
3
votes
0answers
101 views

Print a flag in as little lines as possible! [duplicate]

Just for fun, I wanted to see how little lines I could solve this problem in, using python. I managed to condense my code into 9 lines. ...
32
votes
34answers
5k 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 ...
12
votes
2answers
3k views

How can I shorten this python code?

Here is the code which I want to shorten. ...
58
votes
27answers
8k 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 ...
71
votes
11answers
13k 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 ...
6
votes
3answers
285 views

Slice, Slice, Maybe

Everyone loves slicing in python, as in array[5] to access the fifth element or array[:5] to access the zero through fourth ...
13
votes
7answers
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 ...
13
votes
1answer
2k views

Most common elements of a list in Python

This is a tips question for golfing in Python, which is on topic for main. I'm looking for the shortest way to get all of the most common elements of a list in Python, in the shortest way possible. ...
21
votes
1answer
411 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: ...
8
votes
6answers
1k views

Dungeon of botdom

– of crisis and martyrdom (that's the subtitle because subtitles are cool) In this king-of-the-hill challenge in python (yup; you need go no farther to know you may not submit in java), you need to ...
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 ...
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 ...
2
votes
0answers
53 views

Skip or delete lines between two keywords in a file using python [closed]

The problem is I need to skip or delete all the lines between the two keywords in a file and the keyword pair will appear many times in the file.The file looks something like this ...
19
votes
2answers
776 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 ...
10
votes
1answer
2k views

Shortest python A + B program with input and output

Some of you may say that I am wasting my time, but this task does not give me peace for more than 500 days. It is required to write a program in Python 3.x that takes two numbers as input and displays ...
6
votes
4answers
476 views

How can I golf this code to count all perfect squares below 100?

I'm trying to strip down my python code to be as few lines as possible. The task: count how many square numbers are below 100. Here's my code so far: ...
34
votes
6answers
4k 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
4answers
470 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” ...
12
votes
5answers
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 ...
35
votes
4answers
2k 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 ...
-2
votes
1answer
292 views

Import this into your own language [duplicate]

The Zen of Python must surely be one of the most beautiful Easter eggs in any language. (It is created by writing import this) So why not create a version for your ...
10
votes
1answer
608 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
3answers
465 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 ...