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

Buckshot roulette, a turn based roulette game in Python [closed]

I have started this game to represent Buckshot roulette. I have been coding in python for maybe 2 months and don't know a whole too much about how to undergo this project. This is what I have so far, ...
11 votes
3 answers
975 views

Project Euler #19: Counting Sundays in the 20th century using Pandas

Project Euler #19 asks: How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? I'm hoping I wasn't too off course from the spirit of the exercise ...
7 votes
3 answers
800 views

Create a snail matrix

Statement: Create a script that, given an integer \$n\$, print a square matrix of dimensions \$n \times n\$ with the numbers from \$1\$ to \$n^2\$, arranged in a snail pattern. Example: ...
2 votes
3 answers
65 views

Arithmetic practice script in a tkinter window

I wrote a script that gives the user a quick arithmetic quiz and results using a tkinter window. I am very new to programming, and this is also my first time using <...
5 votes
4 answers
391 views

Optimal Solution for the Four Divisors Problem on LeetCode

I recently encountered the Four Divisors problem on LeetCode and managed to achieve a 100% beat rate with my solution. I'd like to share it with you all and gather feedback on its effectiveness and ...
7 votes
3 answers
918 views

Is this Python/Pygame that draws a maze well structured?

I'm not very confident about this piece of code I created, it basically takes a file like this: ## # ## ## # #B # # # ## ## ## A###### Then proceeds to ...
1 vote
2 answers
114 views

Sum of differences between products and LCMs

I've been trying to solve this Codewars problem https://www.codewars.com/kata/56e56756404bb1c950000992 In this kata you need to create a function that takes a 2D array/list of non-negative integer ...
1 vote
1 answer
49 views

Model validation through the combination of PCA with classifiers

I use the code below for building machine learning algorithm. In the end, I need to validate model performance. However, I am confused if I do it correctly or not. ...
-3 votes
0 answers
21 views

Turning a table with months and days and adding the month with total number of days into a dict for each month [closed]

What is the better way to do this? I was having issues where when I used one loop it would only contain the last addition rather than the total. Ex. August will have 4, 3, 5 and it would end with 5. ...
3 votes
1 answer
107 views

Model View Controller in Python

I have attempted to do Model View Controller in Python for learning. First, I am not sure if dictionaries are faster than lists for about N = 30 (20 to 40) items. Second, how maintainable is this ...
3 votes
1 answer
231 views

GUI Tic Tac Toe game with UNBEATABLE AI players

This is an updated version of my Tic Tac Toe game with AI players, it is related to GUI Tic-Tac-Toe game with six AI players - part 1: the UI. But that question is more than two weeks old and no one ...
7 votes
4 answers
3k views

Pad a ragged multidimensional array to rectangular shape

For some machine learning purpose, I need to work with sequences with different lengths. To be able to process efficiently those sequences, I need to process them in batches of size ...
-4 votes
0 answers
23 views

Request for Assistance with DFS, PMI, and Pairwise Constraints Implementation (Python) [closed]

I've been working on a Python implementation combining Depth-first Search (DFS depth = 2), Pointwise Mutual Information (PMI), and Pairwise Constraints (ML must link and CL cannot link). While I've ...
2 votes
1 answer
53 views

Python sklearn rolling yearly validation

I am trying to implement a simple modelling pipeline with rolling c.v., making use of the TimeSeries split. The code is provided below with a working example dataset. (please don't pay too much ...
6 votes
1 answer
143 views

Approximation search source reconstruction localization algorithm

Goal To determine the coordinates of some signal source in a 3D space, given the coordinates of four observers and the time at which each saw the signal, as well as the velocity of the signal. ...
4 votes
2 answers
1k views

A kinematics (SUVAT) equation solver for micropython

I made a Python program for my calculator's micro-python that lets you enter a value for any 3 of these variables: ...
4 votes
2 answers
289 views

Optimizing Multiple Subplot Visualization and Navigation in Matplotlib

I have implemented a class in Python using Matplotlib for visualizing multiple subplots along with navigation buttons to move between different subsets of data. Each subplot displays a contour plot ...
2 votes
1 answer
2k views

Multiple choice quiz with difficulty and topic options

This is my code for a multiple choice quiz (MCQ) with difficulty and question options. It uses a namedtuple for the questions, and puts them in an array, and uses a ...
8 votes
2 answers
673 views

Image puzzle PIL tkinter

I'm a hobbyist programmer (just having fun). I created a small script that takes a photo and resizes it, then cuts it up into 24 pieces. The chunks are transposed and made into buttons. Your task is ...
1 vote
3 answers
143 views

Inventory Management System

I'm trying to learn Django by building an inventory management system, here's my final models.py. I'm looking for feedback! ...
4 votes
1 answer
64 views

Centre of mass based TSP solution (2)

I updated the code to a working state. My previous question. Even if this code working I wonder if I can write this code in a more efficient way. Maybe less for loops Etc. ...
-1 votes
2 answers
96 views

Breadth-first search [closed]

there are some problems in this code that I can't think of a solution. It does work for print(real_path,count) but the real thing I want is return real_path,count and for doing this I need to make the ...
4 votes
2 answers
157 views

Matrix Factorisation class packaging methods for factorisation of explicit & implicit data matrices using Gradient Descent, SGD and ALS

Attached below, and also as this GitHub gist, is code for a Python class I wrote as part of a personal learning/portfolio project on collaborative-filtering recommender systems via matrix ...
8 votes
3 answers
834 views

Split String Function Implementation in Python

For learning purposes, I am trying to implement Python's built-in function split. I am new to Python, and I know that str.split ...
5 votes
3 answers
306 views

Find highest number of swaps within a set of possible duty swaps with networkx and PuLP

With huge help from Stack Overflow, I have created code which takes possible swap matches with given weights as an input and outputs a set of matches which gives the highest weight with the highest ...
8 votes
3 answers
478 views

python cprofile decorator

If you're anything like me and sometimes write pretty slow code... you may end up going back to API's or scripts and wanting to profile an endpoint/method to see what is taking so long - so I can ...
3 votes
1 answer
86 views

Sorting songs with the ability to save and resume partial sorts

I listen to a lot of music (~4k h/y) and managing thousands of songs is a bit of a challenging. To improve my listening experience I want to better organize my collection. One thing I want to do is ...
7 votes
1 answer
241 views

Colorful Subgraph Dynamic Programming Solution and a Naive One

Given a graph \$G(V, E)\$ with vertices \$V\$ and edges \$E\$, where each vertex is associated with a single color from a set of colors \$C=\{1, 2, ..., k\}\$, we define the following problem: Problem ...
8 votes
3 answers
275 views

Refactor the code which performs "cross-product", "reduce", "product of list" and "sum of list"

I have come up with a sequence of steps to find the maximum product of y positive numbers which add up to x. But the program is ...
-1 votes
2 answers
123 views

Drayage Webscraper: Limited to table structure

This is my first working scraper. I'm sure a lot can be improved. My biggest question is how can I better specify what data to pull? All the data I'm currently grabbing is needed, but I couldn't ...
5 votes
2 answers
714 views

A small REPL in Python

The REPL offers functionality for inserting, deleting, and searching within a red-black tree, along with the capability to generate an ASCII or graphical representation of the tree structure. ...
5 votes
1 answer
318 views

Compute and plot a 2D vector field with radial symmetry in Python

I computed a 2D vector field \$\mathbf{U} = (u(x,y), v(x,y))\$ with radial symmetry, parametrized as \$(u,v) = a(r) (x,y) + b(r) (-y,x)\$, where \$a(r), b(r)\$ are given as solution of an IVP which I ...
5 votes
1 answer
183 views

Testing binary search module, based on bisect

The bisect module can only find the index to insert an element. I created a simple class based on the bisect module for precise searching and covered all the edge cases that came to mind with tests. ...
4 votes
2 answers
113 views

Finding cheapest route (turn costs 500, going straight costs 100)

Story Original link written in korean Jordi, the architect of a construction company, received a request from a customer for an estimate for the construction of a raceway. According to the raceway ...
1 vote
2 answers
104 views

My first code: Blackjack game

After just learning a bit of Python, I tried coding something myself. I attempted to do a game of Blackjack, which is far from perfect, and still doesn't have some rules a real game has (in my game an ...
3 votes
1 answer
387 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 ...
5 votes
1 answer
104 views

Making sklearn's decision trees easier to traverse

scikit-learn's decision tree structure is difficult for me to navigate. I would prefer to have functionality like tree.left, ...
8 votes
3 answers
10k views

Parking Lot OO Design (Python)

Please review my code for parking lot design. I am new to OOD concepts, It will be great to have some feedback on OO Structure of the solution. Requirements considered: The parking lot can have ...
10 votes
3 answers
1k views

Function that returns activation function, as well as its derivative

I have the following function that returns one of several functions along with their derivative for training neural networks. Can this function be improved and made more pythonic in any way? Are there ...
4 votes
1 answer
118 views

Finding the earliest time we can schedule

We have a list of \$j \in \{1,\ldots,N\}\$ jobs with a processing time \$p_j\$ and a demand \$d_j\$ between 0 and 1. These are real numbers. Jobs require the fixed demand over the entirety of their ...
3 votes
0 answers
46 views

Text-Based Multiplayer RPG Engine using Flask & React

As a final project for a software engineering bootcamp, I decided to make a text-based multiplayer RPG game engine. Using Python, Flask, & Flask-SocketIO for the backend and React & TypeScript ...
4 votes
2 answers
286 views

Simple version of NEAT

I have wrote my own simple version of NEAT and want to improve the code for mainly performance (training and runtime of a generation). This simple version of NEAT aims to perform somewhere near as ...
4 votes
1 answer
121 views

Repetition-Limited Multiset Coefficient function

This is a simple multiset coefficient calculator in python, that will calculate both standard multiset coefficients, as well as multiset coefficients with repetition limits, used initially as part of ...
-2 votes
1 answer
56 views

Code that loops through a df and joins two seperate dataframe and sink into delta lake, how do I make it run faster?

I have two dataframes: Budget and Forecast. For those dataframes, I'm trying to create snapshot record by joining with temp table snapshot_to_collect for loop. I'm ...
2 votes
2 answers
83 views

Checking file header for magic number in Python

I wrote this to check files for successful compression with LZ4. Any advice is welcome. Particularly regarding raising exceptions and error handling. ...
1 vote
0 answers
55 views

Django Google Authentication App Using OAuthlib and DRF

Context I've been working on a Django-based Google Authentication application, designed to manage OAuth authentication without relying on libraries such as ...
3 votes
2 answers
279 views

Text-based blackjack in python

This is code for a text-based game of Blackjack. I would also like advice on how can I implement a betting system in this program. For example starting with an amount of chips and choosing how much of ...
12 votes
2 answers
1k views

Space shooter game using pygame

I have made a space shooter game in pygame with over 800 lines of code (including blank lines). The aim of the game is to kill all of the ships in the game. You start with a small spaceship and one ...
4 votes
1 answer
108 views

Optimising Graphed Rewards

Relates to this and this. Simply put; I want to be able to distribute a total reward among network, where the distribution diminishes according to the depth of the network but the total sum of ...
3 votes
1 answer
81 views

Multiline string concatenation

Problem Description Printing the result of adding two numpy arrays together is very ugly. Here we will use these numpy arrays as an example: ...

1
2 3 4 5
309