Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

3
votes
2answers
50 views

A game of dice, but avoid number 6

I was invited to play a game of dice which I had never heard of. The rules were simple, yet I think it would be perfect for a KotH challenge. The rules The start of the game The die goes around the ...
-3
votes
0answers
68 views

One way traffic [on hold]

In a certain region all places are the one by road. But one-way traffic for all roads; if you are allowed to go from A to B via the direct route, you can not along that road from B to A. If you ...
0
votes
0answers
57 views

Troubleshooting recursion code (AoC) [on hold]

This is for Advent of Code, Day 17. I literally got it ALMOST working in five minutes, but then it won't terminate ... I've been messing with it for the good part of the day and still can't figure it ...
18
votes
19answers
4k 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 ...
-13
votes
15answers
246 views

Sequence and Series

Use any programming language to display numbers divisible by 1000000 that are greater than 1 and less than 10000000. Each number should be displayed in new line. Your program should display exactly: <...
56
votes
37answers
9k views

Robot Roulette: High stakes robot gambling

Final Standings +----------------------------------+---------+---------+---------+----------------------------+ | Name | Score | WinRate | TieRate | Elimination ...
3
votes
4answers
447 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 ...
8
votes
4answers
361 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
508 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
376 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
4k 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
131 views

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

i don't know if it can be made shorter than this ...
7
votes
2answers
467 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
841 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
984 views

Reducing a Python Script [closed]

Does anyone have any suggestions as to how I could shorten this program? ...
6
votes
4answers
647 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 ...
10
votes
2answers
466 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
193 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,...
34
votes
23answers
3k 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. ...
30
votes
3answers
536 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 ...
11
votes
2answers
227 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
306 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: ...
13
votes
1answer
466 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
98 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
4k 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
2k views

How can I shorten this python code?

Here is the code which I want to shorten. ...
57
votes
27answers
7k 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 ...
69
votes
11answers
12k 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
274 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
990 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
1k 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
358 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 ...
15
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
48 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 ...
20
votes
2answers
758 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 ...
11
votes
1answer
1k 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 ...
7
votes
4answers
450 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: ...
31
votes
5answers
3k 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
385 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” ...
13
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 ...
34
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
271 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
590 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
443 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 ...
4
votes
1answer
700 views

Need help golfing Python 3 code to calculate volume & surface of spheroids in <150 bytes

I want to write a function golf(C, A) that takes the height (C = 2*c) and the width (A = 2*a)...
13
votes
4answers
541 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: ...
3
votes
0answers
308 views

Most creative/diabolical/entertaining way to find Python version? [closed]

There are important differences between Python 2 and 3, so it's often useful to know which version of the interpreter is running your code. The straightforward method is something like this: ...
3
votes
2answers
341 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 ...