All Questions
Tagged with python-3.x algorithm
325 questions
4
votes
3
answers
1k
views
Coding exercise to represent an integer as words using python
Context:
Convert a non-negative integer num to its English words representation.
Example 1:
Input: num = 123
Output: "One Hundred Twenty Three"
Example 2:
Input: num = 12345
Output: "...
4
votes
1
answer
81
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.
...
4
votes
4
answers
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 ...
5
votes
1
answer
118
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 ...
3
votes
1
answer
358
views
Python function to find the count of digits of numerals in base n up to a given limit
This is a simple exercise to find the count of digits of all numerals in a given base up to a given limit (not including the limit), I wrote it to determine the ratio of bytes saved when the numbers ...
0
votes
2
answers
310
views
Python script that makes generalized Ulam spirals
This is a Python script I wrote to generate generalized Ulam spirals.
I call the spirals generated by my code Ulamish spirals, they are one dimensional polylines that cross all two dimensional lattice ...
5
votes
3
answers
804
views
Python script to split overlapping ranges, version 4
This is the fourth iteration of a Python script I wrote that splits overlapping ranges, and this is the fastest version I have wrote so far, and also a version that works for all inputs. I have done ...
1
vote
2
answers
345
views
Find the shortest distance between two letters
I've been tasked with finding the distance between two letters in the alphabet. Below is my solution. My main concerns are if there is any shorter way to do this, and if there are any edge cases I'm ...
2
votes
0
answers
125
views
Snake game with constant time complexity algorithm
https://github.com/speedrun-program/constant_time_snake_game
Memory efficient snake game with O(1) algorithm for snake movement and bug placement.
Three grids are used:
a grid representing the game ...
3
votes
1
answer
1k
views
Calculate overlap of two datetime objects
I would like to find out how much time lays in between 22.00 and 6.00 o’clock for a given event per day.
So if the event start at 21.00 and ends at 23.59 the result would be 1.59. For a start at 22.00 ...
6
votes
2
answers
314
views
A selection sort implemented in Python
I'm not proficient at Python. My question is twofold: whether this selection sort algorithm implementation for number lists could be considered clean code (in the sense of being modular and ...
1
vote
0
answers
27
views
Optimizing search algorithm for guessing list of strings via function returning bool if substring is in list [duplicate]
I trying to figure out the most effective way to accomplish this task:
A function, check(), contains a list of strings. Calling the function with a string as ...
2
votes
3
answers
384
views
Most effective search algorithm for guessing list of strings via function returning bool if substring is in list using Python
I trying to figure out the most effective way to accomplish this task:
A function, check(), contains a list of strings. Calling the function with a string as ...
6
votes
1
answer
196
views
ASCII art smoke wisp generator
I've written a Python function that generates a smoke wisp/genie-tail shape using all 26 (and only 26) letters of the alphabet. Here are some examples of the kind of ASCII art I'm trying to produce:
...
1
vote
2
answers
712
views
Find the cheapest flight to reach from source to destination
As the input you get the flight schedule as an array, each element of which is the price of a direct flight between 2 cities ...
3
votes
2
answers
220
views
Saving Scraped Data to a File
When scraping and saving data into a file, Which method is more efficient when saving scraped data to a file?
open the file first, scrape, and save the data all ...
4
votes
3
answers
271
views
Increase efficiency of stick cutting algorithm
I have this typical code interview type problem.
Suppose you have a stick of length n. You make X cuts in the stick at places x1, x2, x3 and so on. For every cut in X cuts you need to print the length ...
3
votes
1
answer
113
views
Segmentation of list to minimize the largest sum
I have written the following code for diving a list 'seq' into 'k' segments such that the maximum sum of each segment is minimized. I have used dynamic programming to solve this problem. But my class &...
7
votes
3
answers
1k
views
Algorithm which finds the count of values are smaller to the right than the current one (in the list)
I have to solve the task which finds the count of values, are smaller than the current one, located to the right.
Here is the example:
...
3
votes
1
answer
210
views
generate combinations from list of numbers
generate all combinations from list of numbers,the combinations can be pair of two numbers
example 1 : list of 2 numbers [1,2]
[
[[1],[2]],
[[1,2]]
]
example 2 : ...
2
votes
1
answer
4k
views
The smallest positive number that is evenly divisible by all of the numbers from 1 to 20
Problem description:
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of ...
5
votes
2
answers
2k
views
Find non-overlapping pairs of elements in a list whose difference is less than some given threshold
I have the following task: Let us have a list (denoted by L, and for simplicity, the elements come from the interval [0,1]). We are given a parameter (denoted by C), and we want to find as many pairs ...
6
votes
3
answers
503
views
The shortest path between airports
Airlines need to provide their customers with flights to every corner, so they need an app that allows them to do so. The customer must be able to transfer when there is no direct connection.
The ...
1
vote
1
answer
99
views
Synchronization \ backup of directories and files (Python)
stackexchange members. I'm just learning to write. I set myself a task a few days ago and today I completed its implementation. The task was to create a "backupper" (I was inspired just by ...
6
votes
1
answer
751
views
LFU Cache implementation in Python 3
Need some feedback on this implementation of LFU cache in python3.
Original problem : https://leetcode.com/problems/lfu-cache/
Would really appreciate some feedback on readability, understandability ...
2
votes
1
answer
291
views
Python functions that calculate first n terms of a given order of Fibonacci sequences
I have written two functions that calculate the first n terms of a given order o of Fibonacci sequence, and return the result as ...
4
votes
1
answer
585
views
Lingo (word-game) guessing strategy
My friend and I are kind of noobs in Python and are looking for ways to improve. We thought it would be cool if we could get some feedback on the following code. It is for a game named "Lingo&...
4
votes
4
answers
1k
views
Count numbers that are the sum of three perfect cubes
From numbers from 1 to 100, count the numbers where the following equation is satisfied with 1 <= a <= 100, 1 <= b <= 100, and 1 <= c <= 100: $$a³ + b³ + c³$$
I have this:
...
3
votes
2
answers
95
views
Cleaning a game logs list to find the frequent action triplets and the busy user
I have these game logs which I need to clean, process and find the frequency of game actions. These game actions should be a triplet. In the below given list,
...
5
votes
1
answer
9k
views
Simple IP address subnet calculator
In this task I had to create simple IP address / subnet calculator in Python. I'm just wondering how you see this problem.
There is my code:
...
1
vote
2
answers
297
views
Generate a string that is not present in a list
For a college assignment, I was tasked with the following (excerpt from provided code file):
Find one string of characters that is not present in the list. You are provided a function
that loads the ...
3
votes
1
answer
841
views
Divide array into three disjoint sets with equal sum
Problem definition : Array partition problem
Given an array of positive integers, divide it into three disjoint subsets having equal sum. These disjoint sets cover the complete array.
Example
Input: [...
0
votes
2
answers
452
views
Python Quicksort Implementation with duplicates
Please critique my implementation of Quicksort in python 3.8:
...
5
votes
3
answers
219
views
Detect loop in rectilinear path
Given a rectilinear path, find if it has a loop. A rectilinear path is made up of made up of alternating horizontal and vertical segments.
Input => Ordered set of points representing ra ectilinear ...
2
votes
2
answers
283
views
Find the highest product of three numbers in a list (Python)
I wrote the below as a solution to:
Problem
Find the highest product of three numbers in a list
Constraints
Is the input a list of integers?
Yes
Can we get negative inputs?
Yes
Can there be ...
1
vote
0
answers
134
views
Generating Unique Subsets using Bitmasking
Given an array arr[] of integers of size N that might contain duplicates, the task is to find all possible unique subsets.
Link to the judge: LINK
Note: Each subset should be sorted.
Example 1:
...
1
vote
1
answer
54
views
Python Prime Search Slower Optimization
I have these 2 version of code. Both are looking for all primes under a certain ceiling value.
The first is testing against the set of all odd numbers.
The second in testing against the set of all 6k-...
2
votes
0
answers
575
views
3Sum - leetcode
Question:
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set ...
5
votes
1
answer
142
views
Quicksort Algorithm Speed
I have made a sorting algorithm in Python 3. This sorting algorithm is a modified optimized median of 3 quicksort, with network sorting for lists of size up to 16 and for finding the median. I have ...
1
vote
1
answer
535
views
Finding longest word in a sentence
A problem from Coderbyte. Find the longest word in a sentence after removing the punctuations and if two or more words are of same length return the first word
Below is my solution:
...
3
votes
1
answer
534
views
Optimizing Dijkstra on grid Python (Microsoft Interview)
Given a square grid of size N, each cell of which contains integer cost which represents a cost to traverse through that cell, we need to find a path from top left cell to bottom right cell by which ...
0
votes
1
answer
96
views
Algorithm For Longest Palindrome
I made an brute force algorithm to find the longest palindrome within the string. This palindrome is a substring of the input. Below is the code.
...
0
votes
1
answer
105
views
How to reduce time complexity of obstacle calculation in a grid map? (python)
How can I change the function calc_obstacle_map(self, ox, oy) to get an equivalent result that runs as fast as possible? The time complexity of ...
2
votes
1
answer
44
views
Combinatorial Geometry Algorithm (synthetic)
I created an algorithm to solve in synthetic geometry. This problem states that these points are a scalar (real) number. The goal is to first make the possible triangles from the line segments. Then ...
5
votes
1
answer
207
views
Algorithm Optimization -- Automatic Dimensionality of PCA
I have implemented (rather, edited the implementation of) a technique to automatically detect the optimal number of dimensions for PCA, based off of this paper.
This was inspired by ...
4
votes
1
answer
200
views
Another Sliding Window - find each `k-size` window the maximum num. from an integer array/list
I've searched this site and find many similar questions, but not exactly the same as the one I'll post.
This attempts to solve Leetcode 239:
You are given an array of integers ...
3
votes
1
answer
172
views
Find minimal modulo-sum of pairs from two lists
Recently I appeared for an job challenge organised on HackerEarth.
Question:
There are two teams T1 and T2. Power of each player is represented in array. The rules of game are as follow.
There are ...
3
votes
0
answers
2k
views
Optimizing recursive backtrack search algorithm in Skyscraper puzzle
I'm trying to solve a skyscraper puzzle for 6x6 boards using constraint programming and then backtracking. My solution works but is too slow, for certain cases, it takes up to 2 seconds and I was ...
5
votes
2
answers
348
views
Chess Dictionary Validator from Automatic Boring Stuff with Python
This project is in chapter 5 introduced me to a whole new world of dictionary data structure. To put it briefly, I want to know whether the choice I took between lists, tuples, and dictionaries could'...
3
votes
2
answers
586
views
LeetCode 8: String to Integer (atoi)
I'm posting a solution for LeetCode's "String to Integer (atoi)". If you'd like to review, please do. Thank you!
Problem
Implement atoi which converts a string to an integer. The function ...