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
3
votes
1answer
40 views

Code to remove columns in a (fasta) file with more than 90% '-' characters in that column

Context: This script reads in a fasta file, counts the number '-' characters, alphabetic characters and total characters for each sequence (sequence descriptions are on lines marked with '>'), and ...
4
votes
0answers
48 views

Slow parsing of mutiple XML files to CSV

The code below is written as part of a parser, which files would be also added here: XMLParser, parser, Name, GenericElement, attrib It reads an XML files from a folder, turn them into a dataframe ...
5
votes
0answers
52 views

Count number of unique adjacent cells in a matrix

I want to write an algorithm to count how many islands are in a given matrix. Consider for example: ...
4
votes
0answers
37 views

XML parser + bs4 combo

I just wrote a short function to parse SO jobs XML feed and return dicts containing information about each job entry. Now, each job entry page is visited to grab info not present in the xml feed (...
9
votes
2answers
922 views

DeliciousSoda: A simple parser for robots.txt files

This is my first big project that I've decided to release publically. It's available on my Github, and through pip3: ...
5
votes
3answers
54 views

Project Euler 21: Sum of amicable numbers optimisation

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b ...
7
votes
2answers
707 views

Simple converter: Is it better to use a function within a function?

Below is an example of a simple converter. In the first example temperature I have used a function within a function. In the second example ...
6
votes
2answers
97 views

A Neural Network

I programmed a Neural Network in python. Feedback every kind is appreciated. I tried to use some vectorization but it turned out to become quite a mess. Because you can't append to numpy arrays I ...
11
votes
4answers
1k views

Reverse Polish Notation (RPN) Calculator

I am trying to solve a codewar challenge (link): Your job is to create a calculator which evaluates expressions in Reverse Polish notation. For example expression ...
0
votes
1answer
35 views

simple python music quiz [closed]

I'm currently learning python and created a simple music quiz. the rules of the game are as follows: The user has two chances to guess the name of the song If the user guesses the answer correctly ...
2
votes
1answer
37 views

Test a single-line file for punctuation and newline

I have written the following program to read from a file (entire input in single line) and check for: ASCII punctuation characters a final newline ...
13
votes
5answers
2k views

Advent of Code 2019: Day 4

Advent of Code 2019: Day 4 I'm doing Advent of Code this year. Below is my attempt at day 4: Problem Part One --- Day 4: Secure Container --- You arrive at the Venus fuel depot only to ...
5
votes
0answers
109 views

How can I simplify the Python code dealing with lot of images and multiplications?

With some basic knowledge of Python and referring a lot of sources, I have written the code below. But it takes half an hour for execution. How can I reduce the time? I read about vectorization but ...
5
votes
0answers
39 views

Python - Displaying messages in ArcPy/IDLE

I have created a wrapper for displaying messages with ArcPy and IDLE (as that is all I have available to myself due to certain circumstances) as I frequently use both to test and develop new tools. ...
9
votes
2answers
601 views

Manipulating list values in python

I wrote a function that I use to preprocess pandas dataframes before running them through a machine learning model. The function works perfectly, however I don't think it's the most pythonic way to ...
5
votes
1answer
72 views

IntCode computer in Python 3

I'm using advent of code 2019 to start learning Python. I've read the Python style guide, and I'm trying to write readable and reusable code. This piece of code is an implementation of the IntCode ...
9
votes
5answers
2k views

Python Code for List of Month Names starting with current month

I am looking for a cleaner, more pythonic way to get a list of the name of the months starting with the current month and ending 12 months later. For example, it's December so my list should be <...
6
votes
1answer
65 views

A Backup Script for my Linux Server/NAS

This is my script. I want it to run on a weekly basis on my linux server on a Raspberry Pi 4, to back up all files that may have changed. On the GPIO are just LEDs. It requires a program called ...
-1
votes
0answers
42 views

Image Classifier CNN model configuration

While trying to make an image classifier, I stumbled upon some codes which I then modified to make my own image classifier. To evaluate these classifiers, the code will show the confusion matrix, ...
3
votes
0answers
25 views

Transfer an array of unknown size from C to Python using Ctypes

I am new to Ctypes. I am writing a piece of code that brings an array of unknown size from C to Python using Ctypes. Unknown size here mean that Python initially doesn't know the size of the array. C ...
3
votes
0answers
30 views

Batching calls to server in python with asyncio

I've written this decorator to avoid too many requests to a backend. Did I get it right? Do I need some locks or can I assume some things are atomic / thread safe because this is asyncio? ...
8
votes
1answer
282 views

Brainfuck compressor in Python

Just for fun, I created a script called TinyBF which can compress Brainfuck programs into a format half their size. For example, take a famous hello world program clocking in at 78 bytes: ...
3
votes
2answers
95 views

Find all files that match a filename pattern in a folder tree

I am an old python newbie here, migrating from fortran77 ;) Still unsure of the python terminology at the moment :( The python3 script below fetches a large list of matching files in a folder tree, ...
3
votes
4answers
91 views

Finding the elements with the most neighbors

I have a sample list which contains nodes and their neighbors, if they have any. The task is to find the node which have the highest number of neighbors. ...
8
votes
0answers
84 views

Draw a UML-style class hierarchy of Python's Abstract Base Classes with Python, python-graphviz, and graphviz

Motivation Python's Abstract Base Classes in the collections.abc module work as mixins and also define abstract interfaces that invoke common functionality in ...
-1
votes
1answer
56 views

NOt optimized code,how can i fix it? [closed]

I dont now how to optimize me code. The game i created lags 15 fps, how i can it be reduced and optimize.This my code, I try to do optimize, but i cant. Please help: ...
4
votes
0answers
54 views

Multi-platfform process handling

The goal of this code is to handle processes in multiple platforms, Linux initially. Windows later. The high level handler starts and stops a process or a list of processes. The platform specific ...
2
votes
1answer
56 views

Finding the market price for each order at the time it was placed

I have two datasets. The first dataset “insideMarke1” contains the Best Bid / Best Ask (=BBA) data for a given day, starting at 8.00 am. Every time the BBA is updated, there is a new row with a new ...
0
votes
2answers
51 views

Printing text based on divisibility of number [closed]

The following piece of code will print a text based on the divisibility of number. If the number is not divisible by 3, 5 and 7, then the number gets printed. Does making use of a f-string make the ...
-4
votes
0answers
20 views

Do I have to type math.sqrt() every time rather than just sqrt() etc? [migrated]

Possibly a bit of a silly question; I did some basic Python stuff a few years ago and I remember having to type import math, ...
0
votes
1answer
57 views

Insert a digit to maximize the given number

Was asked in an interview today - You are given an integer (in finite range [-N, N]) and you wish to maximize the number by inserting somewhere the digit '5'. My solution - the idea here is going ...
3
votes
1answer
50 views

Find number of horizontal brush strokes to paint a skyline

Asked in interview today: You want to paint a skyline on your wall using only horizontal brush strokes. You are given an array of numbers, each representing the size of the column/building in that ...
0
votes
0answers
32 views

Oracle of Kevin Bacon (aka Erdos number) [closed]

I have implemented oracle of Kevin Bacon by creating an edge table between the id of movies and actors and BFS. The run time even for an actor with Bacon number 2 is vast (>10mins). The SQLite query ...
3
votes
3answers
65 views

Random Function Bias Tester

I wanted to create a short little piece of code so that I could see the biases in the random function. ...
2
votes
3answers
41 views

Handling singularities in squircle parametric equations

In order to plot a squircle using its parametric equations: The equations can be found in page 9 of this this article: Fong, Chamberlain. "Squircular Calculations." arXiv preprint arXiv:1604....
1
vote
1answer
52 views

Match a multi line text block

I want to count the occurrences of a multi line ASCII pattern within a file. The pattern looks like this: a1 b2 c3 The file I want to search looks like this: (...
1
vote
1answer
42 views

Python multi-threaded kubernetes watcher

My program (which eventually will be containerised) should be able to do the following: continuously listening for deployment and daemonsets in kubernetes (2 threads) collect info needed (like ...
3
votes
0answers
57 views

Middleware which lets developer maintain dynamic context

Problem Statement: Users can chat while the replies of the chatbot are based on past messages. So the chatbot needs to have the context of the previous messages and reply accordingly. Here is ...
1
vote
0answers
71 views

Aligning the playing time of a video with the observed time within the video

I have a video that displays a soccer match. Video file match.mp4 consist of some opening commercials, the actual match and the half-time end/break. As a result, ...
10
votes
6answers
1k views

Intersection of sorted lists

I don't like this function. Looks too imperative. It takes two sorted lists and returns a sorted intersection of the two lists. There is an implicit assumption that there are no duplicate elements in ...
0
votes
1answer
33 views

Python 3 Simple Xor Pipe/CLI Program

I wrote a simple Python 3 program that takes data from either a file or standard input and xor encrypts or decrypts the data. By default, the output is encoded in base64, however there is a flag for ...
4
votes
2answers
83 views

String replacement with dictionaries — follow up

I was reminded by an achievement notification yesterday of this question of mine, posted nearly four and a half years ago. After reading through my own code and the review posted, I decided to re-...
2
votes
1answer
37 views

Log the diff of a Apache httpd config file and reload the service with Python and systemd

Using systemd's .path unit to run a Python script whenever a httpd config file is changed. The Python script creates a backup of ...
2
votes
1answer
68 views

Single player Hangman in Python

This program is yet to become user-friendly and is still in development, but I wanted to improve the current code. ...
3
votes
2answers
62 views

Find and replace consecutive string values in a list

Suppose I have a list that holds a list of column names: ...
4
votes
2answers
1k views

Advent of Code 2019: Day 1

Advent of Code 2019: Day 1 I'm doing Advent of Code this year. Below is my attempt at day 1: Problem Part One --- Day 1: The Tyranny of the Rocket Equation --- Santa has become stranded ...
5
votes
2answers
266 views

Advent of Code 2019 Day 3

Advent of Code 2019: Day 3 I'm doing Advent of Code this year. Below is my attempt at day 3: Problem Part One --- Day 3: Crossed Wires --- The gravity assist was successful, and you're ...
6
votes
3answers
996 views

Calculate the movement required to get from one angle to another angle on a compass

The goal of the function is to find out how to get from one angle to another angle. Two things are involved: the amount to move by and the direction (+ means turn right, - means turn left). The most ...
1
vote
1answer
87 views

Advent of Code 2019 Day 3, Part 3 [closed]

Advent of Code, Day 3 Problem Definitions ...
2
votes
1answer
49 views

Using Python to search a sorted 2D Matrix

Code Problem ...