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
-1 votes
0 answers
79 views

Paper Soccer Puzzle [closed]

I'm currently working on a project to implement a paper and pencil game, similar to paper soccer, using Python. I've been encountering some issues with my code and was wondering if anyone could take a ...
329 votes
179 answers
142k 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 ...
4 votes
3 answers
421 views

Is there a shortest code to format string

Code Golf Challenge Given a string of 10 characters: 'abcdefghij' I want to format it in this pattern and print it in the console in a minimum number of characters: ...
0 votes
0 answers
85 views

Shortest way to print the first 20 numbers in the fibbonacci sequence with python3 [duplicate]

I was wondering if it was possible to make this code even shorter than it already was. f=lambda x:f(x-1)+f(x-2) if x>1 else 1 [print(f(i))for i in range(20)] ...
5 votes
3 answers
402 views

Valid python function invocation signatures

Background In Python, function arguments are defined within the parentheses following the function name in the function definition. There are different ways to present function arguments, and they can ...
8 votes
6 answers
611 views

KotH - Floating Point Prisoners Dilemma

Background So, nowadays the popular Prisoner's Dilemma variant is the Prisoner's Trilemma, where there are three options. I can easily see that being increased, so I figured Why settle for three ...
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
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: ...
8 votes
1 answer
414 views

Self-referencing tuple in python

Goal: create a self-referencing tuple satisfying x == (x,) in any version of Python. Standard libraries, matplotlib, numpy, pandas only. Answers meeting this extra ...
18 votes
19 answers
5k 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 ...
7 votes
4 answers
1k views

Bigram frequencies in text

Introduction This challenge is about producing a code to count the frequency of bigrams in a text. A bigram is two consecutive letters in a text. For instance 'aabbcd' contains the bigrams aa, ab, bb, ...
-3 votes
1 answer
462 views

What is the ABSOLOUTE shortest way to write this code?

So I got kinda bored and was wondering if it was possible to make this code shorter without modifying anything except a file named coin.py... ...
13 votes
16 answers
2k 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 ...
6 votes
2 answers
392 views

Shortening this Code to process nested tuple even further without the use of max()

I have the code: ...
13 votes
1 answer
727 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 ...
8 votes
2 answers
606 views

Inverting string-based binary number in one line in Python

I want to read two strings on separate lines, each string the same length and containing only 0's and 1's, and determine if the first is the one's complement of the second. How succinctly can this be ...
26 votes
15 answers
2k 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 ...
42 votes
17 answers
6k 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-...
4 votes
3 answers
1k views

Python: can I shorten this code further for checking substrings

So the golf question was like this: You are given a number T and then a total of T number of 17-character strings. For each of ...
-5 votes
2 answers
279 views

King of the Hill: Greed Control [duplicate]

King of the Hill: Greed Control What is Greed Control Greed control is a multiplayer round-based game in which in every round, a player bet a number inside a ...
3 votes
1 answer
2k views

Minimum of a Polynomial in Python

What is the shortest amount of code that can find the minimum of an inputted polynomial? I realize that you can import packages like Numpy and others, but using only user defined functions, what is ...
5 votes
3 answers
2k 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 ...
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: ...
7 votes
2 answers
298 views

How to shorten Python code involving the sum of reciprocals?

I have a coding problem that goes like this: Given a positive integer \$N\$, return a list of all possible pairs of positive integers \$(x,y)\$ such that $$\frac1x+\frac1y=\frac1N$$ I already solved ...
-10 votes
1 answer
353 views

The right way to return [[0, 0, 0], [0, 1, 0], [0, 0, 0]] [closed]

Here is the program to return [[0, 0, 0], [0, 1, 0], [0, 0, 0]] [[0]*3,[0,1,0],[0]*3] Try it online! Can you shorten this code?...
1 vote
1 answer
518 views

How can I make this program smaller? [closed]

I am trying my hand at writing super small programs. I wrote a simple "tree" display program for showing a graphical representation of a folder hierarchy. I have made it small in all the ways I can ...
7 votes
4 answers
727 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: ...
10 votes
2 answers
882 views

Can I shorten this Python code for simulating a car driving around

I am trying to write a solution to the following problem using as few characters as possible (to meme my friend and introduce him to golfing). Can any improvements be done to my code? So the problem ...
15 votes
4 answers
945 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, ...
3 votes
3 answers
723 views

How to shorten the Python code? Part II

Code Python 3, 245 bytes ...
20 votes
1 answer
646 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: ...
9 votes
4 answers
3k views

How to shorten the python code?

I'm writing a program that takes a number from input (call it x), loops from 0 to (x - 1) (let's call our iterator y) and prints \$y^x+x\$ for each y. After the loop, it then prints "SUM: N" ...
6 votes
2 answers
423 views

Pushing the limits to make a piece of code solving the K-Goodness String problem shorter

I am trying to push my limits of making a python code as short as possible. I left the readability part way behind. I was attempting the K-Goodness String problem from Round A of Google Kick Start ...
-2 votes
1 answer
361 views

Shortest way to convert a float to an int [closed]

I'm not really sure if this is the right place to ask this. Now, I know I can convert a float to an int with int, but is there some shorter way?
4 votes
2 answers
1k views

Write an infinitely nested generator comprehension

The goal is to create a generator generator [...] generator generator comprehension. The generator must be created via generator comprehension. The following expression must be valid for any number ...
3 votes
2 answers
542 views

Remove duplicates from list and order it by count? (python 3)

How do I remove all duplicates from list and order it based on count? ...
7 votes
2 answers
3k views

How to ignore first user input in Python 3

I have 2 inputs: 1 single number and some numbers separated by whitespace: 1 1 2 3 4 5 I want to loop over the second input, and I don't need the first input. I ...
5 votes
1 answer
262 views

Tips on golfing this Python program to generate a palindrome from a string

I wrote this function in 48 bytes and I wonder, is there any way to write this code 2 characters shorter? p=lambda n,c:(n*c)[:n//2]+(n*c)[:n//2+n%2][::-1] The ...
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: ...
4 votes
2 answers
3k views

Shortest Solution in Python 3 for Caught Speeding - CodingBat

I am trying to find the shortest code in python 3, to solve this problem: You are driving a little too fast, and a police officer stops you. Write code to take two integer inputs, first one ...
22 votes
9 answers
3k 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 ...
6 votes
6 answers
835 views

Let's play Judgement!

In this king-of-the-hill challenge, you will be making a python 3 bot to play the card game judgement! Sandbox Rules Judgement is a hand based game similar to games like rummy. In each round, 7 cards ...
3 votes
4 answers
11k views

List of paths to nested dictionary [closed]

Given a list of nested menu items, ...
36 votes
24 answers
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. ...
16 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” ...
11 votes
13 answers
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 ...
12 votes
3 answers
1k 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
1 answer
389 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 ...
-4 votes
1 answer
240 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 (...
0 votes
7 answers
657 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 ...