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
5
votes
2answers
281 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 ...
5
votes
2answers
58 views

Report time on whole hours

Report time at whole hours ...
8
votes
1answer
283 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
1answer
448 views

Subset pandas DataFrame based on two columns ignoring what order the match happens in

I have two Pandas DataFrames and I want to subset df_all based on the values within to_keep. Unfortunately this isn't straight ...
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 ...
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 ...
-1
votes
1answer
57 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: ...
8
votes
0answers
86 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 ...
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....
5
votes
1answer
1k views

Is this implementation for token based authentication in flask-peewee secure and efficient?

I'm making an app with a Flask API backend using a Flask-Peewee ORM and an AngularJS frontend. The Flask-Peewee ORM doesn't support token based authentication, so I decided to try to implement this ...
5
votes
1answer
12k views

Python - Quiz Game with Tkinter

I have made a little Quiz Game in Python. This was my first experience in programming Gui's in Python with Tkinter. In general, I do not have much knowledge about graphic programming and have acquired ...
-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, ...
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
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
3answers
66 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. ...
3
votes
1answer
51 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 ...
7
votes
2answers
65 views

Calculate implicit dates

I need help to simplify this code about implicit date calculation. The context: I have sentences with implicit and explicit dates. For instance: "From the 5th to the 7th of September 2017, the ...
1
vote
1answer
31 views

Custom model to count user interaction in application

In language dictionary application users can add new words and do other staff with this word such as adding additional information to the word (description, etymology, speech parts, noun class etc.) I'...
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
0answers
58 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
1answer
57 views

Processes Files Using Subprocess and Os

This program I have written automates some of the more manual work in getting the results from my experiments. The idea is that I have folders and subdirectories in my main directory. Some of these ...
6
votes
1answer
1k views

Django Rest Framework Totaling API values in different ways

I am working on a stats website for my softball teams that I manage/play for. Here is my current Stat model: ...
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
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, ...
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
1answer
203 views

Minimax algorithm for tic tac toe in Python

I am using minimax algorithm (for now without alpha beta pruning) for AI in tic tac toe game in Python and Numpy. It's working, but very slow, so I would like to optimize it. A few rules for current ...
8
votes
0answers
129 views

Solving Pentomino puzzles by using Knuth's Algorithm X

I wrote a program which solves Pentomino puzzles by using Knuth's Algorithm X. My priorities were: readability, straightforwardness and brevity of the solution, so I didn't try to squeeze as much ...
13
votes
2answers
2k views

Fishy: An ASCII Programming Language

I've decided to write a very simply output programming language. All the user does is write ASCII values inside ASCII fish, and the interpreter pieces the values together and outputs them. I'm mainly ...
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: ...
5
votes
1answer
43 views

Using Python & Selenium to set Gnome desktop to Google's Chromecast wallpapers

I created a simple script that is used to implement Google's Chromecast wallpaper-images as the wallpaper of my Gnome desktop. The script extract the images from 2 different sources: A static ...
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-...
5
votes
1answer
67 views

Overloading python class constructor

I am trying to create a very basic class: Line as follows: ...
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 ...
4
votes
1answer
74 views

Using composition instead of inheritance and abstract classes

I have a tool that displays data in some format. For example, it can display a list of dogs in a CSV or JSON formats. In the code, I had a lot of places where I had to check the format and display ...
6
votes
3answers
1k 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 ...
4
votes
2answers
427 views

Greed Dice Scoring Game expanded - Python Koans

I've been trying to level up my python skills (more experienced in other languages) by working through the python3 version of Python Koans. After completing the "greed dice scoring project" exercise, ...
2
votes
1answer
49 views

Using Python to search a sorted 2D Matrix

Code Problem ...
1
vote
1answer
87 views

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

Advent of Code, Day 3 Problem Definitions ...
4
votes
0answers
41 views

Querying all items that are in a many-to-many relationship with a child of an object

I have the following Django models setup: ...
1
vote
0answers
22 views

PyOpencv code that displays color, b/w and canny images input from single live webcam camera

I have e single live webcam camera. I have displayed one color image, black and white image and canny edge image. Do you think I should put a scrollbar for the canny edge detection amount to make it a ...
9
votes
2answers
530 views

Call a text-editor from within a Python script to get user input

Motivation I'd like to know how integrate the use of text editors (e.g. emacs, vim, etc.) into command-line Python programs. Features I'd like to be able to do the following: Call a text editor ...
5
votes
1answer
94 views

Display image from live webcam as taken, with four different color filters and in B/W

I have a live webcam windows which means there are six subdivided windows in a single window live. And show pictures in colored and black and white. Is there anyway I can make the code ...
8
votes
3answers
1k views

Temporary queue to prevent duplication

I am making a Reddit bot and to prevent my bot from posting to the same comment twice, instead of making a database, I decided I would rather create a temporary list (or "queue") that once appended to,...
2
votes
1answer
59 views

Unit tests of matrix arithmetic

Is this good way of creating tests? I have no experience how they do it in companies. Ignore uppercase names of variables.. I may change it eventually. ...
-1
votes
4answers
58 views

How to optimize if statement in python from a parsing script [closed]

I have a part of my code I want to optimize. I use if statement to skip the analyze if the values are not the one I want. Just to help the comprehension, in my ...
4
votes
2answers
143 views

Managing user defined and default values for instance attributes

The following works as expected but I wonder if there is a more idiomatic way to check the input kwargs against the user-required (non-defaulted) arguments. It is ...
5
votes
1answer
138 views

process numerical input arguments of mixed ints, floats, and “array-like” things of same length>1

I'm trying to process numerical input arguments for a function. Users can mix ints, floats, and "array-like" things of ints and float, as long as they are all length=1 or the same length>1. Now I ...
1
vote
1answer
77 views

Printing the last two digits of an exponential calculation

I am trying to create a program where a user inputs two numbers - a base and an exponent, while exponent is a binary number. Then prints out the last two digits of the result. My code is working well, ...
0
votes
0answers
77 views

Recursion on graphs - networkx VS adjacency lists with native lists, sets or numpy vectors ( python )

Hope someone can help me out in reviewing this code to make it faster. My goal is to create a graph recursively: query for a node and its neighbors from a db (not present here) add it and its ...