Skip to main content

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
22 votes
7 answers
7k views

Converting JPG images to PNG

I had a task to convert some JPG images into PNG format using Python. I successfully did it but I was wondering if somehow I could make my code better than my current one. Code - ...
Praguru 14's user avatar
22 votes
4 answers
10k views

Regex to parse semicolon-delimited fields is too slow

I have a file with just 3500 lines like these: filecontent= "13P397;Fotostuff;t;IBM;IBM lalala 123|IBM lalala 1234;28.000 things;;IBMlalala123|IBMlalala1234" ...
Mike's user avatar
  • 223
22 votes
2 answers
58k views

Ultra-Beginner Python FizzBuzz ... Am I missing something?

I just started programming in Python this morning, and it is (more or less) my first programming language. I've done a bit of programming before, but never really did much except for "Hello World" in ...
bckwth's user avatar
  • 451
22 votes
6 answers
10k views

Add 'ing' or 'ly' suffix to a string

I have been practising Python Practice questions and came across this question: Write a Python program to add 'ing' at the end of a given string (length should be at least 3). If the given string ...
FoundABetterName's user avatar
22 votes
1 answer
30k views

Check if a directed graph contains a cycle

I am trying to optimize execution time of this function which detects cycle in a directed graph: ...
Raito's user avatar
  • 323
22 votes
3 answers
2k views

Pythons Around the Rose

Based off my Java Petals Around the Rose, I thought it would be good practice to write something again in a different language. Because the language is Python, I named it "Pythons Around the Rose", ...
TheCoffeeCup's user avatar
  • 9,444
22 votes
2 answers
7k views

Simple twin prime finder

I'm really new to coding so I'm looking for a bit of feedback on this twin prime finder. She works pretty decent but I know it could be better. ...
cornelius's user avatar
  • 461
22 votes
2 answers
29k views

Producing ordinal numbers

Is it possible to write this in fewer lines of code? If you input an integer it will output it as an ordinal number if it is less than 100. The below code works perfectly, but I'm wondering if it ...
holaymolay's user avatar
22 votes
3 answers
63k views

Calculate Levenshtein distance between two strings in Python

I need a function that checks how different are two different strings. I chose the Levenshtein distance as a quick approach, and implemented this function: ...
Kyra_W's user avatar
  • 323
22 votes
5 answers
7k views

Battleships game in Python

The idea is, you enter an amount of ships, for example 10. It will then place 10 ships on a 10x10 grid (0-9). You may enter a coordinate, and it will tell you if it is a hit or a miss. A simplified ...
user avatar
22 votes
4 answers
21k views

Finding all k-subset partitions

The following code generates all \$k\$-subsets of a given array. A \$k\$-subset of set \$X\$ is a partition of all the elements in \$X\$ into \$k\$ non-empty subsets. Thus, for ...
spinning_plate's user avatar
22 votes
3 answers
4k views

Console-based hangman in Python

I am a Java + Angular developer. My new job is as a Senior Python developer ... but I've never written a Python program in my life. In order to teach myself the language, I've started writing some ...
Roddy of the Frozen Peas's user avatar
22 votes
3 answers
8k views

Seconds between datestimes excluding weekends and evenings

The basic idea here is that I want to measure the number of seconds between two python datetime objects. However, I only want to count hours between 8:00 and 17:00, as well as skipping weekends (...
Winston Ewert's user avatar
22 votes
1 answer
589 views

Raspberry PI controllable Christmas Tree

After having the scales, thermometer and a coffeemaker connected to the internet, it is finally time for Christmas Tree to become smarter. Hardware 6ft Christmas Tree An addressable WS2811 LED strip (...
alecxe's user avatar
  • 17.3k
21 votes
8 answers
5k views

The lonely product

I am trying to solve the following problem: Given three numbers, a, b, and c, return their product, but if one of the numbers is the same as another, it does not count. If two numbers are similar, ...
user36337's user avatar
  • 323
21 votes
7 answers
4k views

Check if a number N is a power of K

I was asked this question in interview: Check if a number N is a power of K. Example: N = 32, K = 2 => True N = 40, K = 5 => False I wrote following code but got the feedback that, complexity ...
Harsha's user avatar
  • 1,291
21 votes
5 answers
2k views

An OEIS Lookup tool in Python

I'm from PPCG so I was making an esolang and I decided to write it in Python. Eventually it went from an esolang to an OEIS (Online Encyclopedia of Integer Sequences) lookup tool. I'm very new to ...
Downgoat's user avatar
  • 902
21 votes
4 answers
5k views

Why is my Sieve of Eratosthenes using generators so slow

This is my prime sieve based on the Sieve of Eratosthenes. ...
jay jayjay's user avatar
21 votes
2 answers
5k views

Python - Tkinter - periodic table of chemical elements

Inspired by a question on StackOverflow I decided to code a GUI that is simple, efficent and can be used in other projects as well. I wanted to share this code since it probably is usefull to other ...
Thingamabobs's user avatar
21 votes
8 answers
47k views

Program to check if a date is valid or not

I am a novice programmer and am currently learning Python programming by myself through a book. I stumbled upon a question in a chapter and it took me 2 days to figure out a solution for it. Although, ...
mathlover92's user avatar
21 votes
4 answers
75k views

Flattening a dictionary into a string

Given: k = {'MASTER_HOST': '10.178.226.196', 'MASTER_PORT': 9999} I want to flatten into the string: ...
user avatar
21 votes
4 answers
7k views

System that manages employee data for managers

This code is for an employee management system. The goal that I am trying to reach is to allow managers to access data of their employees more easily. The data that is entered is saved to a CSV file ...
RonsJurisdiction's user avatar
21 votes
2 answers
41k views

Turn-based battle simulator

I am embarking on the daunting journey of learning to program on my own two feet and have created a turn-based battle simulator (think Pokémon). The idea came from here. It's very crude and could ...
acardnell24's user avatar
21 votes
4 answers
44k views

RSA algorithm implementation in Python 3

First, a reminder of the RSA algorithm and what my program implements: Take two distinct, large primes p and q Ideally these ...
user avatar
21 votes
2 answers
1k views

Genetic algorithm for “Hello World”

This programme uses a genetic algorithm to get the string "Hello, World!". It can be summarized as follows: Create a random initial population. Let the best ...
Caridorc's user avatar
  • 27.5k
20 votes
9 answers
4k views

Approximating constant π² to within error

This function takes as input float error and approximates constant π² to within error by computing this sum, term by term, until the difference between the new and the previous sum is less than error. ...
Akhil Ghosh's user avatar
20 votes
6 answers
9k views

Python function to map True, False, and None to symbols for display

I have a use case in which I need to check whether a given value is explicitly True or False: ...
Richard Neumann's user avatar
20 votes
4 answers
10k views

Web scraping the titles and descriptions of trending YouTube videos

This scrapes the titles and descriptions of trending YouTube videos and writes them to a CSV file. What improvements can I make? ...
austingae's user avatar
  • 1,080
20 votes
2 answers
8k views

String reversal in Python

This code will take as output any string (long, empty, with spaces...) and will return its reverse. Can I improve this algorithm so that it can be faster? Right now its complexity is \$O(n)\$. <...
Midos's user avatar
  • 311
20 votes
6 answers
5k views

Password validator

I have made a password verification "program" which takes user input and checks whether the password is valid or not. Based on that, a reply is printed. ...
morloq's user avatar
  • 303
20 votes
5 answers
8k views

Count Even Digits In Number

I was asked to complete this CodeStepByStep problem. Here's a quick summary: Write a function named count_even_digits that accepts two integers as parameters ...
Linny's user avatar
  • 10.2k
20 votes
4 answers
86k views

Chess game in Python

I have programmed for 2 months, and I began writing a Chess game. I am a beginner programmer in Python, so please assess my code. ...
Rafail's user avatar
  • 383
20 votes
3 answers
4k views

Planetary simulation in python

I have created a program in python that calculates forces between bodies (i.e earth, moon and a hypothetical moon) and make them move according to the changes in velocity and forces. This is the code ...
Hale's user avatar
  • 303
20 votes
5 answers
3k views

Generating a random MAC address

I need the ability to generate random MAC addresses, so I wrote up a little function that does that: ...
Carcigenicate's user avatar
20 votes
4 answers
2k views

CodeReview question markdown downloader

This is an update to my earlier question From Q to compiler in less than 30 seconds. As with that version, this Python script automatically downloads the markdown from any question on Code Review and ...
Edward's user avatar
  • 66.5k
20 votes
2 answers
13k views

Intersection over Union for rotated rectangles

Problem Statement I am trying to find the intersection over union (IoU) metric for one to several rotated rectangles compared to many different rotated rectangles. Here are some images to help ...
syb0rg's user avatar
  • 21.8k
20 votes
4 answers
17k views

Processing an image to extract green-screen mask

I am using OpenCV to process an image, and in my code, I have to check / edit each pixel separately: ...
Bart Friederichs's user avatar
20 votes
2 answers
1k views

Election vote-counting, result-posting chatbot

In preparation for the end of Code Review's 2015 election, I've built a simple bot to get the election votes, run OpenSTV on them, and post the result to a chatroom. I'm using this Python ...
user avatar
20 votes
1 answer
1k views

Messed up Elevator Management System

In light of our current community-challenge I decided to build an Elevator Management System. Initially I intended to program the EMS like a real-time operating system and the elevators as finite-...
Mast's user avatar
  • 13.4k
20 votes
2 answers
49k views

Proper use of class constants

This question specifically relates to the use of the class constants ABOVE and BELOW in the sample code below. I have a few different classes that look like this: ...
Thomas Johnson's user avatar
20 votes
1 answer
300 views

Rainfall - August 2016 Community Challenge (Hooray for graphs!)

A solution for the August 2016 Community Challenge (Rainfall). I've intentionally left out error checking on the file formatting. This seems like a very obvious graph problem to me - we want to ...
Dan Oberlam's user avatar
  • 7,814
20 votes
1 answer
726 views

Remapping and interpolating 255x1024 array

I have a function that accepts a \$255\times1024\$ array and remaps it to another array in order to account for some hardware related distortion (lines that should be straight are curved by the lens). ...
wes3449's user avatar
  • 303
20 votes
2 answers
346 views

Select external resource to feed data into a processing function

I wrote an application a while back to plan and manage drones races. (Well, it could be anything with identification numbers going through checkpoints.) At that time, communication between the gates (...
301_Moved_Permanently's user avatar
19 votes
6 answers
8k views

Explicit song lyrics checker

To stay in practice with my Python I've decided to write an explicit song checker. It checks each word in the song against an array of 14 explicit words contained in the text file. I've decided not to ...
Linny's user avatar
  • 10.2k
19 votes
4 answers
6k views

Printing all possible sums of two n-sided dice rolls

For my CS class, we were tasked with writing a program that calculates all the possible results from rolling two n-sided dice. For example, the input of 4 would ...
intboolstring's user avatar
19 votes
6 answers
6k views

Premier League simulation

I've created a simulator for the Premier League, which takes in all of the 20 teams and plays them against each other, recording the results and outputting a table at the end. The skill levels are ...
woody101298's user avatar
19 votes
4 answers
3k views

How can I make this tax calculator cleaner?

I recently created this Income Tax calculator based off your filing status (Single, or Married Filing Jointly) and which tax bracket you fall into for the 2020 tax year, taking into account the ...
Erik's user avatar
  • 303
19 votes
4 answers
1k views

Optimising a list searching algorithm

I've created the following code to try and find the optimum "diet" from a game called Eco. The maximum amount of calories you can have is 3000, as shown with MAXCALORIES. Is there any way to make ...
Rlz's user avatar
  • 293
19 votes
5 answers
64k views

Get value from dictionary given a list of nested keys

I would like to get a deeply-nested value, for example {"a":{"b":{"c":"myValue"}} by providing the keys to traverse. I tried chaining together .get() but that didn'...
Martin Burch's user avatar
19 votes
4 answers
5k views

Count number of ways to paint a fence with N posts using K colors

A friend sent me this question, and I'd love somebody to review this and give his opinion. Write an algorithm that counts the number of ways you can paint a fence with N posts using K colors such ...
thomas's user avatar
  • 293