Questions tagged [python]

Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasizes code readability. Use the python tag for all Python related questions. If you believe your question may be even more specific, you can include a version specific tag such as python-3.x.

Filter by
Sorted by
Tagged with
221 votes
4 answers
24k views

Calculate SHA1 hash from binary and verify with a provided hash

I applied for a job and they asked me to write code with the following requirements: Get a "toolbar offer" description from http..update.utorrent.com/installoffer.php?offer=conduit. Parse the ...
user26614's user avatar
  • 2,199
126 votes
6 answers
17k views

How clean is my snow?

I just wrote a snow animation in Python. I think this is fairly clean but I have a few things that I don't like. ...
J Atkin's user avatar
  • 1,790
83 votes
4 answers
22k views

Function to print command-line usage for a program [closed]

The following function works well to print out a help message for the program I have written. It prints a multi-line string message that describes the command line usage with some examples: ...
noisy's user avatar
  • 991
73 votes
5 answers
11k views

Send a tweet to ISP when internet speed drops

I have written an app in Python 3 which monitors internet speed and will send a tweet to an ISP when the speed drops too low. The app has a config file where the ISP and target speeds can be ...
Sir_Steadman's user avatar
73 votes
3 answers
20k views

Monopoly simulator

I was advised by a Reddit user to get my code reviewed on this site. The complete code is on GitHub. ...
ohjuny's user avatar
  • 833
70 votes
2 answers
93k views

Redirecting subprocesses' output (stdout and stderr) to the logging module

I'm working on a Python script and I was searching for a method to redirect stdout and stderr of a subprocess to the logging ...
DracoJem's user avatar
  • 801
68 votes
4 answers
56k views

Flappy Bird game clone for a beginners' programming class

I'll soon begin teaching a beginners' programming class. It's voluntary, so I thought I'd make it interesting by teaching Python programming and then introduce the kids to Pygame, so that they can ...
Timo's user avatar
  • 787
61 votes
10 answers
4k views

Using separate functions for Project Euler 1

I started programming with Java and C++, so I'm used to having a 'main' function that calls other functions that do the actual work. At university I was always told that doing actual computation in ...
Basil's user avatar
  • 881
61 votes
6 answers
5k views

Accurate email syntax validation (no seriously)

So a friend happened to show me how odd and specific the general email syntax rules are. For instance, emails can have "comments". Basically you can put characters in parentheses that are just ignored....
SuperBiasedMan's user avatar
58 votes
9 answers
14k views

Project Euler problem 1 in Python - Multiples of 3 and 5

I'd like suggestions for optimizing this brute force solution to problem 1. The algorithm currently checks every integer between 3 and 1000. I'd like to cut as many unnecessary calls to ...
Robert S Ciaccio's user avatar
57 votes
3 answers
69k views

Dynamic programming knapsack solution

I wrote a solution to the Knapsack problem in Python, using a bottom-up dynamic programming algorithm. It correctly computes the optimal value, given a list of items with values and weights, and a ...
voithos's user avatar
  • 852
56 votes
2 answers
3k views

Counting all the vowels in a passage of text

My son just started high school and in Math, he was asked to do some stats homework. The exercise was to count all the vowels in a passage of text, then answer some questions about frequency. Out of ...
nedlud's user avatar
  • 679
51 votes
3 answers
163k views

Finding the closest point to a list of points

I'm trying to find the closest point (Euclidean distance) from a user-inputted point to a list of 50,000 points that I have. Note that the list of points changes all the time. and the closest distance ...
dassouki's user avatar
  • 1,101
48 votes
1 answer
4k views

The right way to hang a man

Problem: I have seen a few questions around hangman. Usually this is done in a very hackish way, which usually can not be generalized any further. My thought or question is about the creation of the ...
N3buchadnezzar's user avatar
47 votes
8 answers
10k views

Prettify math formula in code

I have a function to calculate the normal distribution in Python: ...
alvas's user avatar
  • 705
47 votes
6 answers
6k views

Raspberry PI controllable car (code for a 6-year old)

I've been trying to show my 6-year old daughter a way to build and create a simple toy car. We've got a 4-motor chassis kit, Raspberry PI and a motozero shield that can operate these 4 motors. We've ...
alecxe's user avatar
  • 17.2k
44 votes
3 answers
2k views

We'll be counting stars

Lately, I've been, I've been losing sleep Dreaming about the things that we could be But baby, I've been, I've been praying hard, Said, no more counting dollars We'll be counting stars, yeah we'll be ...
Simon Forsberg's user avatar
40 votes
6 answers
22k views

Random IP Address Generator

randip.py I know there are probably better, easier ways to do this. It was just a bit of a learning exercise for the sake of familiarising myself with Python. It takes a single argument (positional ...
voices's user avatar
  • 749
40 votes
4 answers
4k views

Someone thinks poorly of my server log parser

I have just been informed that the following code written by me is extremely poor. I have absolutely no idea why. It is memory efficient, and looks clean to me. But still the feedback is very poor. I ...
user3001408's user avatar
39 votes
4 answers
5k views

Beheading of knights, who survives?

We have a round table of n knights, where n is some positive integer. These have failed to satisfy the expectations of the mad ...
N3buchadnezzar's user avatar
38 votes
7 answers
8k views

Increase performance creating Mandelbrot set in python

I created a program in python that generates an image of the mandelbrot set. The only problem I have is that the program is quite slow, it takes about a quarter of an hour to generate the following ...
Ian's user avatar
  • 589
38 votes
2 answers
5k views

Markov country name generator

I wrote a country name generator in Python 3.5. My goal was to get randomized names that would look as much like real-world names as possible. Each name needed to have a noun and an adjective form (e....
DLosc's user avatar
  • 485
38 votes
1 answer
7k views

Python package for handwriting recognition

I am currently writing my bachelor's thesis about on-line handwriting recognition. This is not OCR, because I have the information how a symbol is written as a list of pen trajectory coordinates (x, y)...
Martin Thoma's user avatar
  • 1,195
37 votes
5 answers
6k views

Beginner's snake game using PyGame

I am a complete beginner when it comes to programming and writing games and this is my first ever game. I made it with python 3 using pygame library. I would ...
RandomDude_123's user avatar
37 votes
5 answers
4k views

Sorting strings by length - functional Python

I'm trying to port this little F# snippet while staying pythonic: ...
Mauricio Scheffer's user avatar
36 votes
3 answers
2k views

Bush Wanderer - code intended for teaching

I am teaching computing to an AS class and this was their assignment: Bush Wanderer Task 1 Create a 5x5 grid of hashes(#) where the player is an ...
Toby Adams's user avatar
35 votes
10 answers
9k views

Search for a multiword string in a list of strings

Unlike Perl, you can't to my knowledge match a regular expression inside an if statement in Python and assign the result to a variable at the same moment. This ...
Thor's user avatar
  • 465
34 votes
3 answers
12k views

Using default None values in Python when assigning split() to a tuple

Let's say I want to parse audio track information into two variables like this: '2/15' -> track = 2, num_tracks = 15 ...
janos's user avatar
  • 111k
33 votes
3 answers
9k views

A* pathfinding algorithm too slow

I'm trying to make a pathfinding algorithm for my project. I need it for a simulation and I need it to be fast!!! I watched a few videos explaining the algorithm online and came up with the code ...
André Rocha's user avatar
33 votes
7 answers
9k views

Tiny image scraper for xkcd.com

This came out on the spur of the moment, as a quick and dirty tool to have the job done. This is a simple image scraper for immensely popular and legendary comic website inspired by a Python Easter ...
baduker's user avatar
  • 1,348
32 votes
7 answers
7k views

Car crash reaction time

In physics class we are doing an investigation thing. Some of the numbers might seem plucked out of thin air but they aren't. The maths is right(I think) I just want some feedback on the code. ...
Danny's user avatar
  • 321
32 votes
5 answers
5k views

Chess game for my students

I teach programming, and I currently teach my class about inheritance, abstract classes, and mixins. I wrote this code as an example, and I want to be sure it is as good as possible before I release ...
Infinity's user avatar
  • 505
32 votes
1 answer
8k views

100 gunmen in a circle kill next person

I am very happy because I solved this problem with very little code: ...
Caridorc's user avatar
  • 27.4k
32 votes
8 answers
5k views

Project Euler Problem 45

As a self-teaching Python beginner for almost 4 months, I have mostly been doing online challenges including Project Euler problems. Problem 45 asks: Triangle, pentagonal, and hexagonal numbers ...
ifsoMarcus's user avatar
32 votes
3 answers
44k views

Overlapping rectangles

I received the following question in a technical interview today (for a devops/SRE position): Write a function which returns true if the two rectangles passed to it as arguments would overlap if ...
Jim Dennis's user avatar
31 votes
2 answers
36k views

Password checker in Python

Using Python 2.7.12 I wrote a simple little script, psk_validate.py, that prompts the user for a potential password and checks if it has upper and lower-case ...
MadHatter's user avatar
  • 837
31 votes
1 answer
2k views

Using argparse module within cmd interface

I've created an application that uses a cmd interface. It has multiple levels, and the number of available commands and their complexity is growing. As such, I need to generalise argument parsing - of ...
Ramon's user avatar
  • 411
30 votes
6 answers
4k views

Python web-scraper to download table of transistor counts from Wikipedia

I have been looking for answers for how to easily scrape data from Wikipedia into a CSV file with Beautiful Soup. This is the code so far. Is there an easier way to do it? ...
Sofelia's user avatar
  • 301
30 votes
6 answers
72k views

Checking for balanced brackets in Python

I'm solving HackerRank "Stacks: Balanced Brackets" in Python. A bracket is considered to be any one of the following characters: (, ...
vinibrsl's user avatar
  • 647
29 votes
4 answers
7k views

Sieve of Eratosthenes - Python

I've been doing a lot of Project Euler lately and just wanted to make sure my implementation was as good as it could be. Does anyone have any suggestions to speed this up? ...
Igglyboo's user avatar
  • 447
28 votes
6 answers
9k views

Matching a generated string of random letters to an input

I made a program in Python and wanted it to be faster, so I wrote it on C# because it's compiled. To my surprise, the Python program is much faster. I guess there is something wrong with my C# code, ...
bobpal's user avatar
  • 383
28 votes
5 answers
6k views

Fishing simulator

I'm new to Python, for a school project I created a "fishing simulator". Basically, it is a use of random. I know that my code is repetitive towards the end, but I don't know how to simplify it. <...
myang0630's user avatar
  • 564
28 votes
9 answers
145k views

Asks the user to input 10 integers, and then prints the largest odd number

I have written a piece of python code in response to the following question and I need to know if it can be "tidied up" in any way. I am a beginner programmer and am starting a bachelor of computer ...
Ryan Schreiber's user avatar
28 votes
5 answers
6k views

A first "Hangman" game in Python

I am new to Python (it's my first language), been coding for a couple of weeks now. I have already made a couple of simple scripts to download and manipulate some financial data, but lately I thought ...
Mike's user avatar
  • 381
28 votes
6 answers
2k views

1, 2, Fizz, 4, Buzz: FizzBuzz

I just recently started working on python (interesting language, actually), and, as I always do when I learn a new language (with the exception of BrainFuck, that's too hard), I write a FizzBuzz ...
TheCoffeeCup's user avatar
  • 9,454
28 votes
4 answers
17k views

Repeatedly multiplying digits until a single digit is obtained

I've written code that solves a problem from codewars.com. My code passes all the tests on the codewars site. It's a simple enough problem to solve naïvely, but I would love to improve the structure. ...
Caitlin Quintero Weaver's user avatar
27 votes
7 answers
8k views

Python program to calculate GCD

...
Delfin Biju's user avatar
27 votes
6 answers
115k views

Project Euler #1: Multiples of 3 and 5

Challenge Description: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of ...
MTHL's user avatar
  • 535
27 votes
6 answers
12k views

Simulating an OOP Coffee Machine

Today I learnt the basics of OOP. I have tried to apply them to this coffee machine project. However I'm still a beginner, and so I feel my code can be improved. Are there any tips, trick or other ...
Anonymous's user avatar
  • 1,204
27 votes
4 answers
40k views

Simple card game to learn OOP

My goal was to get my hands dirty in OOP by designing and using classes and getting started with inheritance and other OOP concepts. I have written a very small code to play a card Game called "War"....
Inquest's user avatar
  • 405

1
2 3 4 5
307