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
1 vote
2 answers
80 views

Saving multiple files in python

I have this stupid code block below to save multiple files in Python. It is kind of repetitive (but it works). I was wondering how to improve it, notably: Is it worth it to write a function for two ...
Lucas Morin's user avatar
0 votes
1 answer
54 views

Advent of Code 2023 - Day 8: Haunted Wasteland (Part 1)

Description: The task involves navigating a haunted wasteland on a desert island using a camel. The objective is to escape from the current position (AAA) to the destination (ZZZ) by following left/...
Harith's user avatar
  • 5,844
5 votes
3 answers
2k views

Leetcode problem 8 (atoi). Why is my solution so slow?

This is my solution to Leetcode problem 8, the atoi problem, in Python 3. This is just a generic atoi routine that converts any ...
Ranch Mayonaise's user avatar
2 votes
1 answer
134 views

String and bytes range generators

character_range aims to be an equivalent of the native range for str and ...
InSync's user avatar
  • 165
2 votes
1 answer
122 views

Codewars 4 Kyu kata: "Most frequently used words in a text"

This was the first kata I found hard so I wanted to ask about it. Any ways I could I have done it better? here's the question -> Most frequently used words in a text ...
Afasys's user avatar
  • 21
1 vote
1 answer
75 views

Sports league creation script

I'm building a little game that lets you generate a random sports league with random teams and random rosters. What are some ways that I can improve the readability and performance for this code? Main ...
code writer 3000's user avatar
0 votes
1 answer
80 views

Efficient way to load Spacy model

Here, is there any efficient way to load en_core_web_mdmodel without importing whole spacy module? It takes a significant amount of time to load the python program. ...
Shyam3089's user avatar
  • 111
2 votes
3 answers
724 views

Find median value of two Sorted Arrays

To improve my coding knowledge can you please give me suggested changes on my code? ...
Subha Rajagopal's user avatar
5 votes
1 answer
212 views

Compute the mean of a series of user-entered numbers

Which is more elegant out of two methods as mentioned below mostly from memory prospective? Both methods achieve the same result, but from a memory perspective, In Method1, we are storing the running ...
Karn Kumar's user avatar
3 votes
2 answers
64 views

Paragraph structure template generator refactoring

This is an app that generates random statement types, and basic conjunctions. How to improve its readability and function? How to deploy this code app online? This is the GitHub repository: https://...
Attila Vajda's user avatar
0 votes
0 answers
84 views

Why is my scraping code so slow?

I'm a beginner with scraping with Selenium. And I wrote a couple of scrips. And when the script is used for a website with 10-15 pages, it works as it should. However, when I use it on a larger ...
hemoglobin's user avatar
4 votes
1 answer
94 views

Covariance of random variables of Dirichlet distribution

I don't find an implementation in the library I work with of covariance function: I give a try at implementing it in Python: ...
SSSOF's user avatar
  • 43
4 votes
1 answer
103 views

Advent of Code 2023 - Day 6: Wait For It

Part One: The task involves organizing multiple toy boat races, each assigned a specific race time and a recorded distance. To surpass the existing record, participants must optimize the duration of ...
Harith's user avatar
  • 5,844
2 votes
2 answers
94 views

Starting with a list of initials and last names, extract full names from string of names [closed]

Starting with the following list of initials and last names: ['Arnold-Ker L.', 'Dutra Da Silva R.', 'Bracciali D.', 'Dlouhy L.'] I need to extract the full names ...
Jossy's user avatar
  • 153
4 votes
2 answers
940 views

Python network manager class using exec()

I wanted to make a small self-integrated API for my PyQt5 application. This class is a tiny chunk of the whole project: ...
KhodeNima's user avatar
  • 349
3 votes
1 answer
72 views

Calculating premium splits for policies

Looking for a better approach to write below transformation using Python. Is it possible to avoid loop and still achieve the desired output? It is too slow for 10 million rows. ...
user278818's user avatar
5 votes
2 answers
97 views

Creating csvs using Pandas on large dataset for document retrieval

I am trying to build a useable NLP corpus but getting bottlenecked by how long the program takes (200 hours). With so much data I know that optimizing my code even a little bit will net me huge time ...
evader110's user avatar
  • 143
2 votes
1 answer
121 views

Advent of Code 2023 - Day 5: If You Give A Seed A Fertilizer (Part 1)

The task involves determining the lowest location number corresponding to a given set of seeds by following numerical mappings for soil, fertilizer, water, light, temperature, humidity, and location. ...
Harith's user avatar
  • 5,844
5 votes
2 answers
182 views

Function to search all words matching a search term

I'm solving a python exercise. Here is a brief description: Please write a function named find_words(search_term: str). It should return a list containing all the words in the file which match the ...
fraber's user avatar
  • 179
3 votes
1 answer
91 views

Finding the number of "block chains" that are a certain length and begin with a certain string

I'm currently practicing for the british informatics olympiad by doing past papers. I was doing question 3 of the 2019 paper: A set of children’s blocks, each illustrated with a single different ...
sbottingota's user avatar
3 votes
2 answers
160 views

Advent of Code 2023 - Day 4: Scratchcards

Part 1: The task involves determining the total points of a set of scratchcards. Each scratchcard contains two lists of numbers: the winning numbers and the numbers the player has. Points are awarded ...
Harith's user avatar
  • 5,844
6 votes
2 answers
102 views

Schedule a series of 1:1 contests so that each entrant plays against every other

My sister is organizing a Majong tournament. She wants to set up a set of round-robin rounds. There will be 16 players at 4 tables; 4 players per table. Players will play 5 rounds. She wants every ...
JDT's user avatar
  • 81
1 vote
1 answer
124 views

Advent of Code 2023 - Day 2: Cube Conundrum

Part 1: The challenge involves analyzing recorded games where an Elf reveals subsets of cubes in a bag, each identified by a game ID. The goal is to determine which games would be possible if the bag ...
Harith's user avatar
  • 5,844
2 votes
2 answers
348 views

Advent of Code 2023 Day 3, Part 2 in Python

Part 2 I'm not sure if my solution covers all edge cases. The puzzle goes as follows: Given some input like this: ...
arodin's user avatar
  • 83
3 votes
1 answer
112 views

Encrypted websocket chat using AES (EAX) and RSA for signing

For learning purposes and because it really interests me I am trying to get a better understanding of cryptography by trying to make my own basic secure chat-application. I posted a first version of ...
O'Niel's user avatar
  • 197
1 vote
0 answers
107 views

How to reduce the build time/cache Docker image layer in subsequent GitHub Actions runs?

I have a Dockerfile with ubuntu:22.04 as a base image and a manual Python installation(specific version i.e., 3.11.1) layer which takes a long time. How to cache ...
shaik moeed's user avatar
1 vote
1 answer
208 views

Implementing Preconditioned conjugate gradient

I have implemented the Preconditioned Conjugate Gradient (PCG) method for solving a system of linear equations in Python and I would appreciate it if someone could verify its correctness since I am ...
blov's user avatar
  • 29
3 votes
1 answer
185 views

Encryption implementation for websocket chat (RSA + AES)

For learning purposes I am trying to make a encrypted chat in Python using Websockets. I am using AES and RSA to make it safe. The idea of the chat is that it's a group-chat. Users can join a chat by ...
O'Niel's user avatar
  • 197
5 votes
2 answers
2k views

How to reduce the build time of Docker image layer in GitHub Actions?

I have a Dockerfile with ubuntu:22.04 as a base image and a manual Python installation(specific version i.e., 3.11.1) layer which takes a long time. Approach 1: ...
shaik moeed's user avatar
2 votes
1 answer
64 views

Python optimization search pythagorean triplets

I have an optimization problem. Be it about code, algorithm or maths, that, i don't know. My goal is to find pythagorean triplets satisfying a = b + 1 or a = b - 1. m and n are the triplets generators....
MarvinLeRouge's user avatar
1 vote
1 answer
75 views

Python Runtime Config

I am implementing runtime configuration for a python package. I am doing this in mypkg.config.__init__.py. The idea is to load the configuration from a JSON file ...
farhanhubble's user avatar
3 votes
2 answers
447 views

Hardware API, primarily for Error handling

I have a piece of equipment that I am interacting with. The manufacturer provided an SDK and some simple examples of how to interact with the device. However, I need to write an API for interacting ...
Michael's user avatar
  • 303
1 vote
1 answer
86 views

Extra 0's after rounding [closed]

In a project i am doing for AP CSP, I am attempting to make a calculator that can do sig figs, and there is always a ton of extra 0's after i round the number as seen in the picture i attached the ...
typicaljujunon's user avatar
3 votes
1 answer
268 views

Fuel Gauge Python

I was wondering if my code is good or maybe if it could be written better or simpler. The task was to implement a program that prompts the user for a fraction, formatted as X/Y, wherein each of X and ...
Hoodati's user avatar
  • 33
2 votes
1 answer
245 views

Python integer optimization with PuLP involving polynomials that I think can be faster

I have a problem where we are given multivariate polynomials in variables like y_i and z_j and the goal is to express them as sums of products of terms of the form y_i-z_j with nonnegative integer ...
Matt Samuel's user avatar
1 vote
1 answer
45 views

Make a script to call pager duty's API and update on call

I made this script it works, I'm just looking for ways to make it better/reading to get better. ...
Alex Orbit's user avatar
2 votes
1 answer
116 views

Advent of Code Day 2 in Python

problem for day 2 Part 1 You're launched high into the atmosphere! The apex of your trajectory just barely reaches the surface of a large island floating in the sky. You gently land in a fluffy pile ...
code writer 3000's user avatar
0 votes
1 answer
59 views

DB Layer of a project management CLI

I am (re)writing a project management CLI and want to put it on a solid foundation concerning the database connections and testing. My focus was to build a DB class which is easy to test and could be ...
Koala's user avatar
  • 103
3 votes
1 answer
61 views

Simulating a Moving Average Process

I was studying stochastic processes and thought to independently simulate a moving average process. ...
insipidintegrator's user avatar
1 vote
1 answer
91 views

Managing users via SQLAlchemy DB model (updated)

I am currently in the process of writing a system application that will be operated by multiple users. This will require individual users to access the application while limiting access to those that ...
Michael's user avatar
  • 303
0 votes
0 answers
66 views

Is this iterative inorder traversal algorithm well-known?

I was concerned about the approach in Morris traversal algorithm and came up with simpler solution that uses parent pointers in nodes. The constraints were following: Time Complexity: O(n) Space ...
Max's user avatar
  • 1
2 votes
1 answer
239 views

Finding the longest path on a grid with obstacles from the top to the bottom

I have a rectangle of . (platforms) and # (obstacles). I can start at any platform in the top row and go to the left, right or ...
Aliaksei Badnarchuk's user avatar
6 votes
1 answer
83 views

Optimizing __getitem__ for a bioinformatics script in Python

I'm writing a script for a bioinformatics application in Python that iterates through several sequences looking for amino acids in specific positions to calculate their frequency in relation to a ...
Eduardo Menotti's user avatar
2 votes
0 answers
87 views

NLP pre-processing function optimization as it is extremely low on 92Mb data set

I have a data set that is of 300,000 rows approximately and two columns, each row contains a string, some might be larger than others. All in all, the data set in a ...
Louis's user avatar
  • 121
10 votes
4 answers
2k views

Number to text in Python

I'm kind of new to programming and I would need a suggestion whether there is a better way to solve this exercise. I need to write a function dict_of_numbers() that ...
fraber's user avatar
  • 179
1 vote
0 answers
48 views

Waiting time display app in Python

This application is for restaurant patrons to know how long they have to wait in line. We would like to design a more sophisticated GUI interface, but do not know how to go about it. Please let me ...
Mako's user avatar
  • 11
3 votes
1 answer
123 views

Simple Tic-tac-toe in Python

I am a beginner programmer who coded a simple Tic-tac-toe in Python for practice. Any feedback, especially regarding best practices and tidying up the code, is appreciated. ...
smwt's user avatar
  • 187
1 vote
1 answer
480 views

Is it a correct way to read a file contents using paramiko (SSH, SFTP client)?

Is my implementation of _read_contents (3 lines) fine, or does it create a potential problem (e.g. passing part of an object that has to be destroyed, or a ...
Yulia V's user avatar
  • 635
2 votes
2 answers
77 views

Dictionary additional function implementations

I've written a utility class BetterDict to help me on some of my other projects. I've implemented the &, ...
Linny's user avatar
  • 10.1k
3 votes
1 answer
145 views

Pentomino solver in Python

When I was a child, I used to play Ubongo board game, recently I discovered Pentomino puzzle and I wanted to create a custom solver in python for it. Here is what I got: ...
Riomare's user avatar
  • 31