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
4
votes
2answers
464 views

Is it good style to separate out two related if-statements?

The following code is a working solution for the balanced parentheses problem. I'm really just wondering about coding style here when it comes to separating out related if-statements. When there are ...
2
votes
0answers
57 views

Can we make a worse automatic card shuffling machine?

So I've been droodling on a blackjack project, and one of the issues we need to solve is how to shuffle the cards. I've implemented a method that simulates hand shuffling (E.g split the shoe (either 2,...
4
votes
1answer
46 views

Implementation of Singly Linked List in Python

I am learning basic data structures in Python. I'm not confident enough if my implementations are OK or I miss any corner cases. So please check my code for Singly Linked List and Let me know where I ...
6
votes
1answer
258 views

A Python-program that makes successive API-calls

I wrote some code to make calls to a public API. The code is correct as required as usual. My goal is to make the code as self-explanatory, maintainable and robust as possible. I also tried to make ...
2
votes
0answers
26 views

Python: Random forest code optimization

I've been working on random forest algorithm for classification with roulette selection to find best splits. I came up with this homebrew based on this article https://machinelearningmastery.com/...
4
votes
2answers
70 views

Rotating a matrix clockwise by one element without any libraries (Python 3.9)

The question is as follows: Rotate an NxN matrix clockwise by one element and display the new matrix. For example, if ...
1
vote
2answers
45 views

Periodic Table of Elements (Python-tkinter)

This is a code that generates the periodic table where each element is represented as a button and info about the element is produced when that specific button is pressed. I wanted to make this code ...
1
vote
3answers
45 views

Send email to a list of recipients

I wrote code to automate sending emails using smtp in Python. Below is my code I've written. However, I think there might be a simpler way (simplified code)....could you please review and give me ...
-2
votes
0answers
23 views

zero values in dataframe - python [closed]

hey everyone I have a project to do and I will be glad to receive your help the dataframe I am working on is relatively large from Kaggel, it has 250 columns and 22000 rows, the data are from ...
3
votes
1answer
35 views

Collatz conjecture with plots

I have an assignment where I write a Collatz Conjecture program for a series of starting values from 1 to N and make two plots: number of iterations vs starting value and computed numbers vs starting ...
3
votes
2answers
58 views

Cleaning a game logs list to find the frequent action triplets and the busy user

I have these game logs which I need to clean, process and find the frequency of game actions. These game actions should be a triplet. In the below given list, ...
1
vote
0answers
34 views

Django User Model to authenticate using email address and password

This is my second Django project for an actual client and I want some review on one of my important models. This model will be responsible for authenticating users using their E-Mail Addresses and ...
2
votes
1answer
47 views

How do you efficiently calculate YTD values in a pandas dataframe?

I implemented the following code to calculate the YTD sum in Pandas: ...
1
vote
0answers
21 views

Looking for Correct Code Structure for FastAPI and Celery

I have a FastAPI app that uses Celery and depends on DB transactions. I'm a little confused as to where the DB transaction should occur--Whether it should be run inside the Celery task definition, or ...
6
votes
4answers
850 views

Clean up Python function with too many if/else statements

I have a working function with too many if/else statements (pylint). The code works but it's not good. I try to improve it but I have some tunnelvision. Can someone ...
2
votes
1answer
43 views

My code creates a soundboard that can assign a sound to a key on a keyboard while listening to keyboard strokes in parallel

My code works but I am looking for an efficient way to implement the idea with out having to repeat the same code block inside the function Recin() (record Input function is stated in the code) in ...
2
votes
2answers
120 views

Find month with minimum net price change

Question: The interns at Amazon were asked to review the company's stock value over a period. Given the stock prices of n months, the net price change for the \$i^{...
3
votes
2answers
228 views

List comprehension : Add new element in a list

I have a list and I want to made some manipulation on it. This my list : ...
1
vote
0answers
25 views

Pandas rolling evaluation

I want to perform some calculations over rolling periods of a pandas DataFrame and to abstract all this in a dedicated class. The job of the class below is simply, given backward and forward looking ...
0
votes
1answer
17 views

Flask-SQLAlchemy achieve 1 to many relationship that backfills parent when child is updated

Currently I have two tables Plan (plans) and User (users) and there is a foreign key constraint on users which references plans.id When I create a new user I want to assign a plan to that user via ...
1
vote
3answers
58 views

Python: generates Rolling date in reverse manner up to N number

This is my below code which accepts the date in specific format and generates the N number of rolling date which consists the first and last date of each month in reverse manner. ...
14
votes
4answers
3k views

Split a restaurant bill, including tip

Just looking for a little feedback on the below tip calculator I've coded rounding the output to nearest 2 decimal places. Just wondered if it was readable and code friendly? ...
1
vote
1answer
67 views

Slicing multi index DataFrame into JSON object

I have a MultiIndex pd.DataFrame that I generated from a .txt that is forecast model data. ...
7
votes
2answers
474 views

Hangman game in Python with nine possible words

I'm relatively new to Python, and I've been using the language for about a month. I set out to do the Hangman game. I would like to know if there are things to improve in the code of the program I ...
5
votes
1answer
340 views

Simple IP address subnet calculator

In this task I had to create simple IP address / subnet calculator in Python. I'm just wondering how you see this problem. There is my code: ...
3
votes
1answer
119 views

Functools extension library

I wrote a utility library inspired by functools that adds some common operations on functions I frequently use in my projects. As usual I'd appreciate any feed back....
1
vote
0answers
59 views

Use class instantiation to return an instance of a subclass? (for ABC in particular) [closed]

In one sentence Is there a way to use class instantiation to return an instance of a subclass? (for ABC in particular) Setup I have an abstract base class with several sub-classes, which I can ...
0
votes
2answers
118 views

for loop Optimization

I have a sample Json as mentioned below ...
1
vote
1answer
64 views

Better way to create a contingency table with pandas for film genres from a Film DataFrame

From a public dataset available on film rating I created a contingency table as follow. Honestly I don't like all these "for-loops" I think the quality of the code can be definitely improved ...
-1
votes
0answers
19 views

Best practices for binding properties to a callback in kivy [closed]

As a self learner I've found the kivy binding action (mainly in python) a bit confusing. I've seen in many source code (of kivy/ ...
8
votes
3answers
344 views

Implementing a efficient Infix to Postfix using Shunting-yard algorithm

I was trying to implement an efficient (in terms of size of code (such as using one liners) and time and memory-efficient) of Infix to Postfix in python without the use of any external modules i.e ...
7
votes
2answers
910 views

Converting a YouTube embed link to a regular link in Python

I am fairly new to Python, C# is what I usually code in. I have the following: ...
0
votes
1answer
54 views

Composition and lazy evaluation: is the composite class properly designed? [closed]

Let's consider the following assumptions: The Python programming language is used. One composite class is implemented. There are two or more component classes implemented. For instance, the composite ...
7
votes
3answers
1k views

Finding the majority element in an array

A list is said to have a “majority element” if more than half of its entries are identical. In this classical problem the goal consists of determining whether a list a of length n has a majority ...
1
vote
1answer
54 views

Hough Circular Transform

I'm writing a very basic crater-detection code using Hough Circular Transforms. By far, the longest part of my code is actually doing the transform, which involves numerous nested loops/conditions. ...
7
votes
2answers
602 views

First Hangman Game. Need Honest Opinion

Please provide your honest feedback. Thanks Doing Angela Yu's 100 days of code. Have not seen the videos on this section yet. This is just my attempt. here is the link to run the code. Hangman game <...
-1
votes
0answers
26 views

Saving Functions into Package in Jupyter Notebooks

not sure if I'm phrasing my question correctly but here goes. I've been using Python in a Juptyer Notebook on juptyer hub for one of my class. I've written many useful functions that I want to use to ...
1
vote
1answer
45 views

Scan and Output ACLs to Excel in Python

I am working to solve an issue that my company is currently having, We have an extremely large share drive that is over 20 years old. There are so many security groups attached to some of these folder ...
3
votes
4answers
320 views

Find differences between two directories

Coming from another language than Python, I would like to see if my code is "pythonic" enough and follows good practices. It compares two directories, showing all files that are in one and ...
0
votes
2answers
58 views

Fastest way to find common events in 2 time series numpy arrays and calulate synchronisation statistic

Given 2 numpy arrays a1 and a2 (composed only of 0 and 1's), find index locations of all 0's in ...
2
votes
4answers
72 views

The user-facing part of the code base, using Python's argparse package

This question is a sequel to one I asked the other day. The previous question examined the core of my repo; this one examines the user-facing part. If you're curious to see what the repo as a whole is ...
9
votes
1answer
77 views

Periodic Table - Python and tkinter

I posted an answer on StackOverflow about periodic table sometime back and ever since then I've been thinking about improving/optimizing it and worked up a completely different version of it that ...
2
votes
1answer
39 views

Decorator for forward type hint resolution

Background I frequently develop ORM libraries using the peewee framework. On one particularly large ORM library, representing a proprietary data format, I have a lot of coupled ORM models representing ...
3
votes
1answer
65 views

Optimize "Fill magic square" in python with backtracking

I have written a python program with backtracking that fills a n * n magic square. It solves for n = 4 in 4.5 seconds but gets stuck when I run it for n = 5 on my ...
11
votes
4answers
3k views

Zipping two lists with an offset in Python

Walking my first steps in Python from C, I find myself missing pointers from time to time. In this case I want to go through a list processing two element at a time, where those elements are step ...
2
votes
0answers
54 views

OOP Blackjack in Python 3.9.9

I've noticed I'm not the first to post this topic. As a matter of fact, I have taken some inspiration from Victor Nguyen Bao's post to help structure my code. Here's the link to his code: Structured ...
0
votes
0answers
36 views

Time representation [migrated]

I have the class Time, its purpose is to store time in seconds but for user to use "1:2:3" format, I need to be able to do algebraic operation on it instances, since the whole overriding is ...
1
vote
1answer
64 views

In place QuickSort algorithm in Python

I have been searching for ways to make my algorithm more efficient but most answers told me to divide the array into two differente arrays one of less elements and one of greater elements and then ...
2
votes
1answer
55 views

Refine Linked List Implementation in Python (pop, insert methods)

I've recently learned how to implement linked list in Python. Can anyone help me to refine my code of implementing methods 'insert()', and 'pop()'. pop(pos) - remove and return item at position pos. ...
2
votes
1answer
58 views

Tic tac toe win checker in python?

I made a tic tac toe win checker that represents a 3 x 3 board as a list of list with variables X O B as 'X', 'O' and ' '. However the input can be any valid and invalid board of tic tac toe game. e.g....

1
2 3 4 5
286