Questions tagged [recursion]
Recursion is a kind of function call in which a function calls itself. Such functions are also called recursive functions. Structural recursion is a method of problem solving where the solution to a problem depends on solutions to smaller instances of the same problem.
43,977
questions
0
votes
0
answers
16
views
Fibonacci functions called from a script work as slow as without caching
I have a few Fibonacci implementations in a file called foo.py:
def fibonacci(input_value):
if input_value == 1:
return 1
elif input_value == 2:
return 1
elif input_value &...
0
votes
0
answers
4
views
Scalability issue for simulation WSN
I built my own simulation by DES based on Java to simulate WSN algorthims, but I faced a problem in reaching 1000 sesnor nodes. What should I do to overcome this problem by avoiding stack overflow?
1
vote
4
answers
26
views
Python DFS nested dictionary
I've written a function which should be able to search a nested dictionary, using DFS, to find a specific value. The recursive element seems to be working fine, however, when the base case should ...
0
votes
0
answers
11
views
global counter in recursive function applied to pandas dataframe
Following inspiration from Apply function on each row of a dataframe and increment a counter at the same time to apply recursive function with a termination counter to update a dictionary in a pandas ...
-1
votes
1
answer
16
views
What to put in blanks to complete python program (recursion)
I've been at it for hours now yet I still don't know what to put in these blanks. I have an idea on how it will work but I don't know how to apply it. Help!
Basically, I need to find out what are the ...
0
votes
1
answer
18
views
returning dictionary to break recursive loop returns NoneType [duplicate]
I have a recursive function where the input dictionary is updated
def update_dict(d: dict) -> dict:
global counter
print(f"COUNTER:{counter}")
keys=['a','b']
values=[1, 2]
...
0
votes
1
answer
13
views
Value of an array is returned as undefined from recursive function. Why? How could one fix this? [duplicate]
I am trying to push something into an array in a recursive function and return changed array. For some reason whenever recursion is being triggered the returned value is undefined. Simplified example ...
-1
votes
0
answers
12
views
How to get the list of selected items in DP 0-1 knapsack? (Python)
I've seen this implementation of printing the list of items in a 0-1 knapsack, but it's not memoized. I would like to know how to implement the code used in the link using the following dynamic ...
0
votes
0
answers
7
views
segmentation fault in digital root problem on gfg
You are given a number n. You need to find the digital root of n. DigitalRoot of a number is the recursive sum of its digits until we get a single digit number.
Example 1:
Input:
n = 1
Output: 1
...
0
votes
2
answers
32
views
Get all elements of nested lists with recursion
~ from This Edabit Challenge ~
I need to get all the elements of nested lists and put them all in one list using recursion.My code below prints out each element, but how do I save them all to one list ...
-1
votes
0
answers
28
views
For in loop only prints out second item in array [closed]
items = ['first string', 'second string']
html_str = "<ul>\n"
for i in items:
html_str = '<li>{}</li>\n'.format(i)
html_str += '</ul>'
print(html_str)
Why ...
-3
votes
2
answers
46
views
check if number is prime using recursion with only one parameter in c language [closed]
I've got a task in my lesson to write a function that will check if a number is prime, it will return 1 if it is and 0 if it is not and all of that to do recursively. the function will look like that :...
-4
votes
1
answer
67
views
reversing a list in python using recursion [duplicate]
what I have done is -
def reverse(i, j, lstr):
if j == 0:
return []
if j == 1:
return lstr
if i>j:
return lstr
temp = lstr[i]
lstr[i] = lstr[j]
...
0
votes
1
answer
19
views
Graph recursive DFS when cycles exist?
I'm interested in handling DFS in undirected (or directed) graphs where cycles exist, such that the risk of entering an infinite-loop is non-trivial.
Note: This question is not about the cycle-...
0
votes
0
answers
28
views
ChessAI: Why is recursion of a reference of a class somehow capped?
I have made a c++ class to represent a board and i have noticed that if i call a recursive function that has as an argument the reference to a board object, it stops after a while resulting in approx ...
0
votes
1
answer
60
views
Array.join('') is not working as it should inside of my recursive function
I am attempting to use a recursive approach in order to write a function that will take in a binary string, break it apart into 8 bit sections, and return the string translated to ASCII. As far as I ...
-1
votes
0
answers
27
views
Karatsuba for dart (Recursion function)
import 'dart:math';
int karatsuba(x, y) {
var n, m, a, b, c, d, p, q, ac, bd, pq, adcb;
n = max(2, 2);
m = (n ~/ 2);
a = (x / pow(10, m)).floor(); // 56
b = (x % pow(10, m)); // 78
c = (...
0
votes
0
answers
14
views
LC 114. Flatten Binary Tree to Linked List Question
For the LC question here, I am struggling to find where I am going wrong with the recursive approach. I am able to do this problem iteratively, but I am not sure where I am going wrong with the ...
0
votes
0
answers
26
views
When a recursive function returns itself to a caller, who makes that subsequent call?
async function parentFunction() {
try {
await taskFunction(xyz);
return true;
} catch (e) {
console.log(e);
}
});
async function taskFunction(xyz) {
try {
...
-7
votes
1
answer
47
views
getting the following error: `AddressSanitizer: stack-overflow` [closed]
Most of the test cases passed but later I was getting the following error:
AddressSanitizer:DEADLYSIGNAL ================================================================= ==31==ERROR: AddressSanitizer:...
1
vote
3
answers
47
views
Swap two ListNode* in C++
Please help, I am totally lost here. This is leetcode problem no. 24, swap nodes in pairs.
I am getting this error in screeshot.
/**
* Definition for singly-linked list.
* struct ListNode {
* ...
0
votes
1
answer
34
views
Wrong result if a recursive function is called twice successively with different parameters
I have this recursive function:
def coinChange(coins, amount: int, minimum = float('inf'), memo={}) -> int:
if amount in memo: return memo[amount]
if amount < 0: return -1
if amount == 0: ...
1
vote
1
answer
65
views
How to use recursive in Haskell
I'm trying to understand how recursion works in haskell, I'm trying to do a function that multiply using a sum
Something like 4 * 5 = 4 + 4 + 4 + 4 + 4
My current function is
multiplicacao a b
| ...
-2
votes
0
answers
31
views
How would I go about finding a palindrome within a string with spaces (without) using any built-in string methods?
My new function should be recursive and I need to limit string functionality to relational operators, indexing, slicing, in, and concatenation. Here's what I have right now:
palindrome_input = input(&...
0
votes
0
answers
8
views
Calculate Time Complexity of the below Code - JAVA LEETCODE 544 Output Contest Matches [duplicate]
This is the leetcode problem #544 where compression of the list takes place. I'm looking help on calculating the time complexity of this below code.
class Solution {
public String findContestMatch(...
2
votes
1
answer
35
views
Best way to handle recursion with async functions and promises?
The following is pseudocode to illustrate my problem. The parent function must ultimately return a promise when all of the tasks are done (I've omitted the others for clarity). The parent function ...
0
votes
0
answers
10
views
Counting Unique Paths in an Array with Special Rules
Description of the Problem:
I have an array of two-digit numbers going from 00 to 99. I must choose a number at random anywhere in the array; let's call this result r. I may now take up to n steps ...
-3
votes
2
answers
69
views
find sublist by using recursive generator in python
The nested list is like:
nested_list=['a', ['b', ['c', 'd', 'e'], 'f', ['g', 'h']], 'i', ['j', 'k']]
and we can clearly see its structure:
a
b
c
d
e
f
g
h
i
j
k
Here is the thing I want to do:...
2
votes
7
answers
83
views
Deep list count - count lists within lists
I am trying to get the length of a list, but that list can contain other lists that are deeply nested (e.g. [[[[[[[]]]]]]]).
But you have to get any non list elements in the count as well: [1, 2, [3, ...
0
votes
1
answer
15
views
How does the code below give the same outcome as the code that follows it. I am trying to understand recursion
function multiply(arr, n) {
let product = 1;
for (let i = 0; i < n; i++) {
product *= arr[i];
}
return product;
}
A section in the course of freecode-camp states that ...
0
votes
0
answers
24
views
Why do you save recursive call result in variable?
i'm trying learn dfs and saw this code below.
def recursive_dfs(v, discovered=[]):
discovered.append(v)
for w in graph[v]:
if not w in discovered:
discovered = recursive_dfs(w, discovered)
...
0
votes
1
answer
12
views
why key error Recursion component in React
i'm trying to recursion components but react key props is duplicate in root
when using uuid, is fine. i don't know reason, help me
// filterModal.jsx
<FilterGroupCollection
...
-1
votes
0
answers
28
views
build a protobuf message recursively Java [closed]
I'm new to programming. I'm working on creating a Google protobuf message. The message has some nested messages within it and is long, so it would be too tedious to build the message in manual way. My ...
0
votes
1
answer
25
views
Counting files in subfolders and showing the count of each subfolder separately
I have to count files in subfolders, for example, one folder has 5 different subfolders and each subfolder has image data of different days(the images are of different extensions). So i have to count ...
0
votes
0
answers
19
views
Recursion: My Array Keeps Emptying After an IF Statement [duplicate]
The Prompt for the recursion problem is: Get the length of an array using recursion without accessing its length property.
Here is what I have come up with:
function getLength(array, count = 0) {
...
0
votes
1
answer
31
views
Print all the subsequences of a string using recursion
I have tried a approach of print all the subsequences of a stirng using recursion but not able to implement this how can i print all the subsequences of a string in lexicographical order.
INPUT:-
abc
...
-1
votes
1
answer
31
views
How to invert logic on recursive calls? [closed]
How to invert logic on recursive calls?
I have this solve that counts the number of recursive calls. It's working properly. But how can I invert the logic to count number
0
votes
0
answers
17
views
Recursive function with other inner functions in MATLAB
I'm working on coding this particular equation in MATLAB (image attached below)
and I can't quite... figure out how to do this. I'm making separate functions for each part of this, so like a function ...
0
votes
1
answer
42
views
Can We Tell the Function Call Between Instances of Same Class is Recursive?
I had a quiz of the OOP Course that runs with Java language. We were asked to create a data structure for text file input like this:
A->ab|Ca|Ba
C->Bb|aB
B->a|b
to create an output like:
(ab|...
1
vote
2
answers
30
views
Quicksort RecursionError
I wrote a code about Quick sort and the problem is, I get a Recursion Error for large Arrays with, for Example, over 1000 values. However, I don't have the problem with small Arrays. I don't know why ...
0
votes
0
answers
22
views
Reverse number in python - Recursion [duplicate]
I know there are alot like that question, but I want to know depending on my code, what is the bad thing that will not make it work. I dont want to look at answers...
I need to code that if I receive ...
-3
votes
1
answer
25
views
Recursion Function that prints all possible binary permutations
I am trying to write a recursive function that generates all binary permutations with a given input of the amount of 'A' and 'B' elements that a string should have. For example function(a, b) when a = ...
0
votes
1
answer
44
views
Why is my code not dividing inside of the function?
I want to create a hailstone sequence using recursive function. But the even number is not getting divided, I want to know where and what did I do wrong?
#include <stdio.h>
int sequence(int n);
...
0
votes
1
answer
24
views
Return a specific column of the deepest ancestor for all child records
I am researching recursion in SQL Server.
I have the following query and I can't seem to get it to do what I want.
;WITH cte_txn AS (
SELECT
vth_id,
vth_pol_id,
vth_moved_from_vth_id
FROM ...
0
votes
2
answers
30
views
Page Object Modelling - recursion within a method. Cypress
I am trying to create in Cypress some classes and methods for the website I test and one of the methods I managed to create is recursive. The method had worked fine when it was a "loose function&...
-5
votes
1
answer
78
views
C recursive function has extra output [closed]
This output:
4 4 7 4 11 4 7 4 4
Why is this the output from the following?
int f(int x)
{
if (x == 0) return 1;
else if (x == 1) return 3;
else
{
printf("%d ", f(x - ...
0
votes
0
answers
18
views
Mapping ends to roots in a directional acyclic graph (DAG)
I have a table of relations that follows this basic structure:
CHILD_ID PERSON_ID
1 2
1 3
2 4
2 5
2 6
7 8
. .
. .
note that a ...
0
votes
2
answers
47
views
Recursively Iterate through Grid and Return Result in an Array - JAVASCRIPT
I'm struggling with the logic to do the Recursion.
For the grid below I need to search a word and return an array of indexes that form the word if it exists or an empty array [] if it doesn't exist.
...
0
votes
0
answers
23
views
How to reconnect WebSocket without recursion with web_socket_channel package?
There is a way to do a reconnection in the onDone callback, but with recursion:
https://stackoverflow.com/a/66361830/2134488
https://github.com/dart-lang/web_socket_channel/issues/73
Is there any ...
0
votes
0
answers
38
views
Avoiding stack overflow with functional programming
I am trying to solve "the grid search" problem on hacker rank using functional programming. Please view the problem description on hackerrank.
https://www.hackerrank.com/challenges/the-grid-...