# 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.

15,539 questions
Filter by
Sorted by
Tagged with
142 views

### BlackJack in python

I began creating blackjack in python; however, I'm looking to simulate it, so I'm thinking I'm going to need to optimize it so that I don't just leave my computer running for three hours waiting for ...
233 views

### Tic-tac-toe OOP Minimax Algorithm

I wrote a module that I intend to use in my next tic-tac-toe project. All it is is a class that stores the boardstate along with some methods/properties. The biggest thing is the minimax alg. As shown ...
• 97
1 vote
147 views

### Dial up some obscure stats for the Chutes and Ladders game

Years ago, I wrote some code in Perl for the Chutes and Ladders game. I re-wrote the code here in Python. The code simulates one player in a game to see how many spins of the dial are needed to win ...
• 3,892
2k views

### Create a snail matrix

Statement: Create a script that, given an integer $n$, create a square matrix of dimensions $n \times n$ with the numbers from $1$ to $n^2$, arranged in a snail pattern. Example: ...
• 521
436 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 ...
89 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 <...
• 97
1k 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 ...
66 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 ...
• 179
328 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 ...
171 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 ...
103 views

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 ...
• 53
311 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 ...
• 53
723 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. ...
• 9,452
532 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 ...
• 81
874 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,392
135 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 ...
109 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, ...
81 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 ...
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 ...
• 103
126 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 ...
61 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 ...
286 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 ...
• 31
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 ...
• 121
1 vote
73 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 ...
• 9,106
88 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: ...
57 views

...
• 446
80 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. ...
• 53
1 vote
43 views

### Multi-threaded Error Handling Data Fetching Script

I've been working on a Python script that fetches student data from an API using multiple threads for concurrency. The script retrieves both student information and prospectus data and saves them to ...
132 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. ...
• 767
181 views

### Converting a dict to a list + ID

I wrote this code: PROJECTS_LIST = [ project if not project.update({"project_id": project_id}) else None for project_id, project in PROJECTS.items() ] ...
• 151
113 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 ...
• 63
108 views

### Centre of mass based TSP solution

This is the code that I tried to approach to the traveling salesman problem in a new way. As you can see, it does not give such great results. Can I get this idea further, or should I quit to ...
• 53
160 views

### Creating a search function

Write an efficient function in the Python language called SmartSearch. This function will receive a sorted arr array of integers of size $n$ containing at the ...
• 51
564 views

### Extract data from poorly formatted phonebook

The Problem John keeps a backup of his old personal phone book as a text file. On each line of the file he can find the phone number (formated as +X-abc-def-ghij where X stands for one or two digits),...
• 133
1k views

...
• 446
56 views

### A selenium web scraper to package NBA data

I'm building a selenium web scraper for basketball-reference.com that takes a player name and returns data in either a JSON format or Pandas DataFrame object. The class in question is one of many that ...
697 views

I've been programming for 3 months now, and I've just come across Linked List, and I'm not gonna lie, I've been struggling a little. I wrote this code to practice on Linked Lists. The purpose is to ...
• 179
1k views

### Creating an O(n) algorithm for an array of integers

To avoid plagiarism in my university, I am going to modify the problem. Henry likes to jump from building to building. The heights of each building are given as a list of H numbers. We also number ...
327 views

### Minimal AlphaGo algorithm implementation for game 2048, connect4

I'm writing tutorial code to help high school students understand the MuZero algorithm. There are two main requirements for this code. The code needs to be simple and easy for any student to ...
1k views

### Python Breakout game

I came from C# background with strong OOP approach. I've decided to learn Python and Breakout game is a way to cover the basics of Python. I'll be really glad for any feedback and general opinion ...
251 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 ...
123 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 ...
• 41
304 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 ...
• 121
123 views

### Further developing calculating crude death rates and age-standardised death rates

This script is a further development of Finding crude death rate and age-standardized death rates from data. In reflection to the kind reviews I created a module for the data; saved the data in ...
• 299
132 views

### Longest spell to cast from pages of spellbook follow-up

This question is from the PCTC 2022 R2 Past Paper and is a follow-up on my previous question. Previous question I have implemented several solutions suggested, such as creating an array with pages ...
189 views

### Finding crude death rate and age-standardized death rates from data

How can I improve this code's readability and function, for finding crude death rate and age specific death rate? Given are the death rates from chronic pulmonary obstructive disease in 2019, the ...
• 299
567 views

### Longest spell to cast from pages of spellbook

While practicing for a school coding challenge, I came across this problem. My code got the right answers but exceeded the time limited. Any tips for how to reduce the time complexity? https://pctc....
225 views

### Graphs for distributing rewards

I want to be able to distribute a total reward among referral network, where the distribution diminishes according to the depth of the network but the total sum of distributed rewards equals the ...
• 63
97 views

### Referral Network & Rewards

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 distributed rewards equals the initial total ...
• 63