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.

Filter by
Sorted by
Tagged with
0
votes
1answer
26 views

Why is the local variable within the recursive loop overidden? Recursive loops inside For loops for Java

I am writing a program that takes a number n and returns all the possible combinations to divide the group up into pairs. To do this, I use a recursive method which begins with an ArrayList numbered ...
2
votes
2answers
43 views

Recursive function in C to determine if digits of an integer are sorted ascending, descending or neither

I need to write a recursive function that returns 1 if digits of a whole number are ascending (left to right), return -1 if descending or return 0 if neither. My solution attempt returns 0 every time ...
0
votes
1answer
27 views

Recursively accessing key/value pairs of object within objects & combining them together into a flat array

I have an array of objects that contains all my routes. I need to access all object's nested children properties & their properties and combining the together. My array of object looks similar to ...
1
vote
2answers
57 views

Recursive function to find top level parent given an id

Given the following data set: const accounts = [ {id: 2, children: [1,22,69], parentId: null}, {id: 3, children: [140, 122, 580], parentId: null}, {id: 1, children: [4,5,6], parentId: 2}, {id:...
0
votes
0answers
19 views

Are fields of the same type and access level contiguous? [duplicate]

Suppose I have a class with two fields of the same access level and type; if I have a pointer p to the first one, will p + 1 invariably be a pointer to the second? e.g. for a class class A { public: ...
0
votes
0answers
59 views

Solving a simple puzzle in prolog

I solved a puzzle in C and tried to do the same in Prolog but i'm having some trouble expressing the facts and goals in this language. The very simplified version of the problem is this: there's two ...
-2
votes
0answers
25 views

How to make recursive function that returns the number of following numbers in a node, if there is no sequence the function return -1 [closed]

Just like the title says, How to make a function in Java that gets a Node and returns the amount of following numbers there is (years: 2010,2011,2012 (Returning 3)) and if there is no following ...
0
votes
0answers
26 views

Remove items from nested arrays not corresponding to specific criteria

i would like to remove items from nested arrays (looping recursively) keeping only specific items (or subitems) that are present in a separate list (a simple string array). Maybe the splice method can ...
0
votes
2answers
27 views

How does one approach proving facts about non-primitive recursive functions in coq?

I'm trying to prove these two addition functions are extensionally the same, however I can't even prove the simplest lemma for the second one. How does one do this proof over the non-primitive ...
2
votes
1answer
38 views

Is there a better way to write this recursing method in typescript

I'm trying to write a method to find all the files in a folder, including subfolders. It's pretty simple to write using fs.readdirSync, but I'm trying to write a version which doesn't block. (i.e. ...
0
votes
0answers
20 views

Sum of binary tree not functioning as intended with recursion

So my problem isn't understanding how it works, but rather problems with the code. This is the code given for the class in the exam, can't be changed. class Tree(object): def __init__(self, val, ...
-4
votes
0answers
20 views

How to write a recursive-descent parser to evaluate mathimatical expression? [closed]

Expressions that should be solve able: 1 ((2)) 2 + 3 ((4) - 5 + 7)
0
votes
0answers
35 views

Making Recursion More Efficient

I am currently working on a recursion homework assignment and it works for the most part, but when I start searching larger words it takes a long while to process the information. I am unsure of what ...
2
votes
1answer
48 views

Recursion seems to be working only to some level of depth - how to debug such problems?

Im currently writing a program to draw the Sierpinski carpet using recursion. The code of my program is as follows: #include <iostream> #include <stdio.h> const int L = 4; const int N = ...
1
vote
3answers
38 views

Why won't my recursive function recurse through all of the data in the stack?

I am trying to reverse a stack of type int using recursion. I am able to reverse the first entry, but it shows only zeros when I try to print the supposed reversed stack after the recursion takes ...
-1
votes
1answer
13 views

delete all the node value is 'x' in a singly linked list by Python

def dele(node,target): if not node: return if node.val == target: node.next = node.next.next node.next = None dele(node.next,target) return node This code doesn't work. Can anyone ...
3
votes
1answer
87 views

When is tail recursion guaranteed in Rust?

C language In the C programming language, it's easy to have tail recursion: int foo(...) { return foo(...); } Just return as is the return value of the recursive call. It is especially ...
0
votes
3answers
49 views

Highly confused about how this simple recursive function works - how to explain it?

I had to create function to return a number raised to an exponent. I solved it with a loop: function pow(x, n) { let result = 1; // multiply result by x n times in the loop for (let i = 0; i &...
0
votes
0answers
46 views

clarifying how recursion works in c [duplicate]

hi I am learning recursion and I need help to understand how the recursion works I understood how to use it but not all of it I got here 2 functions for example to help in clarifying what my ...
0
votes
1answer
36 views

what the main function in this program returns to os?

#include<stdio.h> int fact(int); int main() { int n=2; clrscr(); return fact(n); } int fact(int x) { if(x==0 || x==1) return x; else return fact(x-1); } It ...
2
votes
3answers
64 views

Repeating a string with recursion, beginner

I'm supposed to write a simple method that returns given string for given amount, seperated by comma (and no comma in the end), with recursion. If there are less than two counts, the return is empty ...
-1
votes
0answers
17 views

Build Dependencies Tree from Data of two MySQL tables with php [closed]

I have two tables(building and researches) for a techtree of a game like TABLE A: (Buildings) +id+building_name+research_needed+building_needed+ TABLE B: (Researches) +id+research_name+...
2
votes
1answer
34 views

Add all generated lists from a predicate into one big list at the end

I have a function which generates one list for each iteration. (so there are many lists generated at the end of the query). Is it possible to gather all the generated list into one big list outside ...
0
votes
1answer
50 views

C++ create objects in the class constructor recursively

Hi I'm working on my homework and I have a problem here. I have a class and definition is: class WebNode { private: char* m_webAddress; char* m_anchorText; WebNode** ...
0
votes
1answer
36 views

How to write recursion in dataframe?

I have the dataframe like this: Price Signal 0 28.68 -1 1 33.36 1 2 44.7 -1 3 43.38 1 ---- smaller than Price[2] # False: Drop row[3,4] 4 41.67 -1 5 42.17 ...
-1
votes
1answer
43 views

why c() does not working in this recursive function in R?

enter image description here I know there exists function 'unique' which works similar to what I want to make, but I want to make this function. I want this function finally returns 'result' which ...
0
votes
2answers
64 views

Recursion in c to print characters

I need to write a function that receive two arguments, n and 2 characters, from void main and then to print the first character n times and the second character n*2. I can only use recursion, not ...
0
votes
1answer
35 views

Keep getting Stack overflow- Euclid's number

I am on the newer side of using c# for coding. I am looking to find the greatest common denominator of two inputted values. There are some numbers that work, such as 100 &200, 50&150, 2&12,...
0
votes
1answer
66 views

Why is my quickSort method giving me a StackOverflowError? Java

Here is my quickSort method. It consists of two separate methods: one that takes a queue as a parameter (1), and one that takes an array as a parameter (2). Then it uses the parameter array to make ...
1
vote
1answer
35 views

Load Google product taxonomy into Firestore using node

I'm trying to load the Google product taxonomy into Firestore documents, which I think primarily means converting it to JSON. This is a sample of the taxonomy: 1 - Animals & Pet Supplies 3237 - ...
-2
votes
1answer
25 views

Write a recursive function that takes array as parameter and return sum of even integers in the array. int sumEven(int*arr,int n) [closed]

I can solve the above problem in another way. But I have struggled so much to do this number by RECURSION and failed. Can anyone help please?
0
votes
0answers
25 views

Recursively calling a function within a function for dynamic programming - Python

Suppose we have a function like def Model(parameters): # x = do something with parameters # y is exogeneous, as in an external dataset error = y-x return error Let's say we ...
-1
votes
1answer
62 views

Why doesn't this function get stuck in an infinite loop? [duplicate]

This function is from my professor's notes: int ints_is_sorted_r(int* a, int n){ return n <= 1 || (a[0] <= a[1] && ints_is_sorted_r(a+1, n-1)); } This is my version with many ...
0
votes
3answers
62 views

Prevent memory allocation in recursive combination generation

(Sorry about the title, it's not the best descriptive) I am playing with graph theory, and generating all possible combinations of a given set of input numbers. Given the input set {2,3,4}, my ...
1
vote
2answers
43 views

Java recursion exponentiation method making recursion more efficient

I'd like to change this exponentiation method (n is the exponent): public static double exponentiate(double x, int n) { counter++; if (n == 0) { return 1.0; } else if (n == 1) { ...
-1
votes
2answers
30 views

Recursive function does not return proper variable, returns None

I am creating a program that accepts an array and reverses it, it must include a recursive function. I am having an issue where it is returning None instead of the reversed array. Running it through a ...
2
votes
1answer
40 views

AVL tree how to balance tree on insert

I want to make a insert function for a avl tree. However the insert function has to be recursive and must be balanced. I have a method to pivot the tree left PivoterAGauche and a method to pivot the ...
0
votes
1answer
33 views

Appending to one list in recursive function

In my code my return value is appended to the list. What would be the way to append all the items to a single list? Right now, every stack creates a list and returns the list. def ...
0
votes
1answer
33 views

How does this recursive function keep track of how many times it has been executed?

Project Euler problem 14 give the following problem: The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the ...
-2
votes
0answers
42 views

This is a Recursion code I'm having an issue with

The Question is - Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C ...
0
votes
1answer
52 views

How to kill and restart the recursive function in javascript

I am working on knockout js. In that i have a recursive function which executes a function every minute. for that am using a timer every 60 sec it will execute also same will be reflecting in the UI ...
0
votes
2answers
49 views

Infinite loop with recursive SQL query

I can't seem to find the reason behind the infinite loop in this query, nor how to correct it. Here is the context : I have a table called mergesWith with this description : mergesWith: information ...
-3
votes
1answer
51 views

Why is my tic tac toe minimax program not working? [closed]

I made a game where 2 tic tac toe AI:s play against eachother on a 3x3 grid. But when I run the code, it says that bestMoveX is being used without being initialized. The problem is probably very ...
0
votes
0answers
35 views

I'm having trouble memoizing N choose K in c++, I don't want the DP solution

I'm learning data-structures and algorithms, I can solve it recursively and I understand I have to add to a memo table to save repetitions, I'm having trouble doing it, some input would be very ...
0
votes
1answer
39 views

While trying to write the recursive version of atoi, what is the meaning of itoa(n /10, s) and why is it advisable to avoid static int i = 0?

I'm sorry if my question is quite vague, because it is without context. So I was trying to solve a question: Write a recursive version of the function itoa(i) which converts an integer i into a string....
0
votes
0answers
39 views

I can't understand the use-it or lose-it concept (change making problem)

def change(amount, coins): if not coins: return float('inf') if amount == 0: return 0 if coins[0] > amount: return change(amount, coins[1:]) else: ...
3
votes
4answers
99 views

Can someone explain, step by step, what this function is doing

def hanoi(n,f,v,t): if n == 0: pass else: hanoi(n-1,f,t,v) print(f"Move disc from {f} to {t}.") hanoi(n-1,v,f,t) hanoi(3,"a","b","c") I'm studying recursion ...
1
vote
1answer
49 views

Wrong answer to dynamic programming problem of finding minimum stair cost

I am trying to solve the following problem on Leetcode: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay the cost, you can either climb one ...
0
votes
1answer
31 views

Nested Cursor & Recursion in PL/SQL

I'm new to PL/SQL and have just started working with cursors. I'm writing a small program to print the recursive trace for an employee -> supervisor all the way to the big boss. Essentially my query ...
-2
votes
1answer
37 views

Why will it be a dead code if I put if( … == null) as my second base case for recursion? [duplicate]

The following code shows an implementation of a insert method of a binary tree(with key and value) that allows duplicates of multiple keys. private TreeNode insert (int key, String data, TreeNode ...