I was just practicing some Linked List questions when I came across something I couldn’t explain. So I was wondering if anyone could help me out. If I have a Linked List with nodes implemented as follows I tried reversing the Linked List initially like so and it came up with an AttributeError: ‘NoneType’ object has no attribute ‘next’ And
Tag: algorithm
Check if a Python list has X number of consecutive values equal to Y
I have a collection of lists each containing 16 items, and I want to find lists with 12 consecutive values either > or < than a specified threshold. For now, I have iterated through the lists and put 1 for values greater and -1 for values less than the threshold, and I used to following to eliminate those that don’t
Creating a graph from a string
Let’s consider a “maze” defined by a string, for example The sign “#” denotes a wall and the sign “*” denotes the possible path. Moreover “S” is the starting point and “F” is the destination. I would like to apply a path searching algorithm to solve this labyrinth for instance Breadth-First Search. I’ve read that that algorithm uses a graph
How to append several phrases of a string to different lists in a loop
it is actually really difficult to write a suitable title for this question, so please correct if you find something more percise. My problem is basically, that I have a txt file with a pattern which i want to sort. Consider the following string So all the lines are seperated and the pattern is the same. What i want to
Python algorithm to approximate closest parallel equivalence of resistors from a list
The formula for series equivalence of resistors: series equivalence = sum(resistors) For parallel it is 1/(sum(1/resistors[i])) I wrote code to return a list of resistors that is closest to a specified target value from a list, within a specified tolerance. So for example series_equivalance([1,2,3,4,5],7,0)will return [5,2]. I want to a function that can do the same for parallel equivalence. How
N-Queens II using backtracking is slow
The n-queens puzzle is the problem of placing n queens on an n x n chessboard such that no two queens attack each other. Given an integer n, return the number of distinct solutions to the n-queens puzzle. https://leetcode.com/problems/n-queens-ii/ My solution: It fails at n=8. I can’t figure out why, and how to have less iterations. It seems I am
How can I determine The numerator and the power from a certain number?
How can I discover or find out which/what number can stand as a numerator and as a power for a certain number for example: 8 is the initial/certain number, but was split to 2 raised to the power of 3 where; Is there a software/algorithm of any sort that can provide an answer to this question for example 64? Answer
Python find the largest square in the matrix dynamic programming
I have a matrix as follows (Python) : where “o” is an obstacle and I need to find the biggest square in this matrix. and replace corresponding ‘.’ with ‘x’ like below found similar questions here(SO), but nothing helped. Answer It can be done with a complexity of O(n²) using dynamic programming. The idea is that you have a bigger
Algorithm for Connected Components of Graph
I am looking for the most efficient algorithm in order to find both the number of connected components in a network, and the number of nodes for each connected component. Example: Given the following inputs: I would receive the following output: This is what I have so far: I have found a way to iterate through the nodes assuming that
Python 3: time.perf_counter() output does not match the programme processing time on Coursera
I have been working on a Coursera assignment, it required my run time to be less than 5.00 . The code is below, but my question is basically about the first and last line, where I was testing the processing time Using the last line of code, my processing time was 3.3251947 However, when I submitted this code onto Coursera,