All Questions
321 questions
2
votes
2
answers
107
views
Iteratively convert an arbitrary depth list to a dict
The input has a pattern, every element in the list is a dict and has a fixed keys
[{'key': 'a', 'children': [{'key': 'a1', 'children': [{'key': 'a11', 'children': []}]}]}, {'key': 'b', 'children': [{'...
2
votes
1
answer
57
views
How to convert a recursion with multiple recursive calls into an iteration? I already know how to do it when there is only one recursive call
Here is how I would convert a recursive function that only has one recursive call (I'll use JavaScript)
function f(n){
if(n>0){
f(n-1);
doStuff(n); //this could be anything
}...
0
votes
1
answer
100
views
How to iterate backwards with a custom iterator in Java using permutations
I am trying to create a a class of Permutation that extends Iterator and adds two methods: previous() and hasPrevious().
It must offer a constructor with one argument: a string containing the ordered ...
0
votes
1
answer
108
views
Durand-Kerner iterative method for multiple roots?
I am looking at the Durand-Kerner iterative method for Polynomial roots. It has a quadratic convergence rate, However when dealing with polynomial with multiple roots the convergence is only linear. I ...
4
votes
2
answers
280
views
How can I learn to convert recursive solution to iterative solution while preserving the space complexity?
I am trying to solve a easy binary tree question to convert the existing binary tree to a sum tree.
The recursive solution for this is :
class Solution{
public void toSumTree(Node root){
...
0
votes
0
answers
29
views
Iterating through a pair of DataFrames, modifying one of the two at each iteration and feeding back the results
I am trying to reimplement a (rather complex) algorithm from R in Python. The algorithm operates on two dataframes (short and long for simplicity) which have this basic structure:
index chrom chr_arm ...
0
votes
1
answer
104
views
Convert recursive merge sort to iterative using stack
I have implemented the merge sort recursively, using linked list, but I am also interested to implement it iteratively using a stack.
This is my recursive implementation:
typedef int element_t;
...
1
vote
1
answer
81
views
Quicksort from recursion to iteration using the general method of stack
I have a recursive quick sort implementation in C and I want to convert it to an iterative one using stack.
This is my implementation in C:
int random_partition(int* arr, int start, int end) {
...
2
votes
1
answer
1k
views
Is there a way to render category tree structure in React iteratively?
Is there a way to render this category tree structure in React iterratively? I'm currently making this flatmap array into a category tree structure iteratively, however, can't seem to find out how to ...
0
votes
2
answers
179
views
How to call class from another class in python
I am pretty new and trying to work on an algorithm challenge (Binary Inorder Traversal). I am trying to go through a tree (iterative) but for some reason there is a disconnect when I call the function....
1
vote
1
answer
73
views
Why is this highly iterative code faster than less iteration using a dictionary (js obbject really...)
This question is specific to the hacker rank problem Largest Permutation
I wrote a solution in javascript that works for most cases (A), but there are some select test cases where it times out. Ok... ...
1
vote
3
answers
248
views
Finding total amount of paths in a triangle grid (iterative without recursion)
i have a given a triangle grid:
Triangle
For every point (i,j) with i+j being even:
Given recursive function
Now i need to write a iterative function that finds all possible paths from (0,0) to the ...
0
votes
1
answer
143
views
Loop Invariants and General Invariants
Let's say I have an iterative algorithm for the summation of numbers from a to b:
def summ(a, b):
c, v = a, 0
while c <= b:
v = v + c
c = c + 1
return v
Firstly, is it ...
2
votes
2
answers
52
views
How to set a number of loop 'for' as a parameter of a function in python to find all the possibility to cut a period in n parts?
Here is a ugly code to find all the possibilities to cut a period in 5 parts. Is there a possibility to create a function which makes it look better, with the number of cut as a parameter ?
I am only ...
-4
votes
1
answer
83
views
Is this python code iterative or recursive?
I have written the following program in python:
def summ(a,b):
return summation(a,a,a,b)
def summation(v,c,a,b):
if (c == b):
return v
else:
return summation(v+c+1,c+1,a,b)...