Join us in building a kind, collaborative learning community via our updated Code of Conduct.

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.

1
vote
1answer
16 views

break for of loop with recursion

I have a function that takes a node and an object. The larger object potentially has property of children and that is an array of similar shaped nodes. The function is looking at the top level, and ...
0
votes
0answers
6 views

Recursion Not Using Updated Values

I am trying to take a 40,000 line CSV file and turn each line into a valid WooCommerce product post in the SQL database for a WordPress site. However, many of the lines share the same information I am ...
0
votes
0answers
5 views

Windows CMD, trying to recursively delete all folders unless they have a certain file type

If I want to go though a directory and delete all folders unless they contain a particular file extension how would I go about this? I tried Robocopy thinking they were empty but the folders all have ...
2
votes
1answer
17 views

Transducer flatten and uniq

I'm wondering if there is a way by using a transducer for flattening a list and filter on unique values? By chaining, it is very easy: import {uniq, flattenDeep} from 'lodash';| const arr = [...
1
vote
3answers
306 views

how to understand the return value in java's recursion

      I have coded a program with java language,but the answer is never the right one, I use the recursion to complete the program,but the return value in the method is ...
0
votes
0answers
38 views

Combination generation algorithm detail

The code below works, displaying a list of combinations. However, I can't figure out why switching the order of the two recursive calls in comb_util breaks the code. Why is this? It looks to me like ...
-4
votes
2answers
75 views

Recursive Function C++ [closed]

Can someone explain this line: return 1+f(x/2, y); Here is the code: int f(int x, int y) { if (x==y) return 0; if(x>y) return 1+f(x/2,y); return 1+f(x,y/2); } I ...
-1
votes
1answer
28 views

Recursion callback mechanism

I would like to implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-pairs of parentheses. Example: input: 2 (e.g., 2 pairs of parentheses) output: ()(), (...
4
votes
3answers
2k views

How to constuct a column of data frame recursively with pandas-python?

Give such a data frame df: id_ val 11111 12 12003 22 88763 19 43721 77 ... I wish to add a column diff to df, and each row of it equals to, let's say, the val in that row minus ...
-3
votes
3answers
64 views

Find child of a object recursively c#

I want to ask you what's the most efficient way of going through each child object of a Parent. For example I have a class which is: public class Asset { public string Id { get; set; } ...
0
votes
2answers
35 views

What the the variables in following program stores?

total_item([],0). total_item([_|Rest],N) :- total_item(Rest,C), N is C+1. This program takes an array as input and count the total number of item in that list. But I am not getting what exactly ...
1
vote
0answers
15 views

Correct use of is/2 predicate in Prolog recursion

I am a Prolog beginner with an imperative programming background. While solving assignments from this site, I bumped into two exercises: the first one regards finding the k-th elements of a list, the ...
0
votes
2answers
531 views

JavaScript How to limit depth of recursion in JSON.stringify

How can I limit the depth of recursion in the function?
-1
votes
3answers
97 views

Generate a Recursive Tribonacci Sequence in Python

I am supposed to create a function to generate a Tribonacci sequence in Python. This function must be RECURSIVE. Based on this, I have this code so far: def TribRec(n) : if (n == 0 or n == 1 or n ...
4
votes
2answers
57 views

How can set a hard maximum recursion depth in Perl?

Although in some cases I might want to allow deep recursions in my code, I want to be able to disable it in certain cases (like while testing). I know that when using the debugger I can use $DB::deep ...
0
votes
1answer
29 views

Inconsistent gmtime in C when recursively traversing directories

I have a C program where I use a recursive function that traverses a directory structure. It takes a time_t variable and creates a gmtime struct from it. The recursive dir function is from How to ...
0
votes
1answer
19 views

recursive xml-parsing function not working as intended

I'm trying to parse an XML document and use the data to build a (simpler) json object of this form: {id: '1', name: 'content-types', children: [{id: '2', name: 'requirements': children: [... and so ...
0
votes
2answers
26 views

Not sure how this recursion works

class Solution: def findDuplicateSubtrees(self, root): self.res = [] self.dic = {} self.dfs(root) return self.res def dfs(self, root): if not root: ...
3
votes
2answers
72 views

parsec.py recursive definitions

I'm loving the simplicity of this library. Sadly I haven't figure out how to make recursive definitions: Consider this minimal contrived example: import parsec as psc digit = psc.regex("[0-9]") ...
-1
votes
0answers
23 views

How to correctly use Scanner in a recursive main method? [duplicate]

I keep getting java.util.NoSuchElementException error. From what I understand, it is legal to call the main method within itself. In the main method, I use a Scanner object, and close it before ...
-2
votes
1answer
70 views

Why can't 'return' return the value from the function?

Here is my code: def getRow(rowIndex): if rowIndex == 0: return [1] if rowIndex == 1: return [1,1] def f(n,r): if n == 1: print(r) return ...
0
votes
2answers
64 views

Recursion in Egg Drop Puzzle

There are n number of eggs and building which has k floors. Write an algorithm to find the minimum number of drops is required to know the floor from which if egg is dropped, it will break. My ...
1
vote
1answer
38 views

How to generate dynamic menu tree using recursion in ASP.Net?

Let say I have below menu picture in my mind. According to the above picture I've created my SQL CTE query and the query returning me the below result. Scenario The scenario is to create a multi ...
0
votes
2answers
45 views

Recursive vs Iterative Functions Python

I am currently learning Python and would like some clarification on the difference between iterative and recursive functions. I understand that recursive functions call themselves but I am not exactly ...
0
votes
2answers
51 views

Return value from recursive method (java)

I have to write a recursive method to sum the figures of an integer. The method works fine, but I don't understand why it returns the last integer instead of the total. import java.util.Scanner; ...
0
votes
0answers
292 views

Render React component in nested list

If we write something like below in ItemsList component: render(){ return( <div> <ul> {this.props.items.map(item => <Item key={...
-1
votes
1answer
25 views

What is the time complexity of a function that makes recursive calls (logarithmic) within a loop?

Something like this - func(n) { if (n == 1) return; else for (int i = 0; i < 100; ++i) func(n/2); } If you can provide a recurrence relation that would be ...
0
votes
6answers
503 views

Checking for palindrome using recursion in PYTHON

I am checking if a word is a palindrome, and I cant seem to get my code to respond. I feel like its pretty sound, but apparently I'm missing something. can someone point out what that may be? def ...
2
votes
4answers
7k views

Python recursive function to display all subsets of given set

I have the following python function to print all subsets of a list of numbers: def subs(l): if len(l) == 1: return [l] res = [] for sub in subs(l[0:-1]): res.append(sub) ...
8
votes
4answers
14k views

C# Upload whole directory using FTP

What I'm trying to do is to upload a website using FTP in C# (C Sharp). So I need to upload all files and folders within a folder, keeping its structure. I'm using this FTP class: http://www....
1
vote
0answers
12 views

Python recursive graph traversal refuses to work and gives out Runtime Error

My code doesn't seem to work at all on CodeForces. When I got RE for the first time , I increased the maximum recursion depth and tried again. There was no improvement. I tried to print out the error ...
0
votes
2answers
29 views

Many-to-many to recursive CTE in SQL Server

As I understand it, any many to many relationship is a hierarchy if you define one part as parent and another as child. I have a situation where I need to get the children of an object that is a ...
0
votes
1answer
28 views

how can skip files when you don't have permission when you want use listfiles at java?

With below code I print all sub directories but if I cannot access one of the sub directory this code breaks and returns an error. How can i fix it? public static void main(String... args) { ...
0
votes
0answers
95 views

How to break the recursion of a tree insert function?

I am trying to make the insert() function of a tree. This function is used to insert the required number according to its order in the tree. My problem is that when I am trying to reach the last left ...
-3
votes
2answers
31 views

Creating recursive nested lists in python

My initial list is like this in pyhton. list_1 = [ ['A', 'B', 'C'], [1, 2, 3], ['X', 'Y', 'Z'] ] I need to convert it to one like this... return = [ ['A', 1, 'X'], ['A', 1, 'Y'], ['A', 1, 'Z'], ...
0
votes
3answers
46 views

Need help understanding a traverse Binary Search Tree method

I came across a traversing binary search tree function but I can't wrap my head around it. Here's the code: public void inOrderTraverseTree(Node focusNode){ if(focusNode != null){ ...
1
vote
1answer
51 views

Gaps in Endless Runner “Treadmill” with Object Pooling

I am working on a 3D endless runner game, where the player is stationary, and the background/obstacles move around them. I am using object pooling. The background is made up of multiple variations of ...
-1
votes
2answers
359 views

Remove recursion when sending variable as parameter to anonymous function in JavaScript

I've encountered this before and I have a fair understanding of scope though not enough. I'm trying to pass a string as a parameter to an anonymous function, inside of a for loop. From my ...
-2
votes
1answer
22 views

How does this mutual recursion logic work?

The below code returns 39, and I can't understand how the logic works to where it arrives at 39. I keep getting 36 or 43. Can anyone list step by step how this program runs? a(1); function a(foo) { ...
0
votes
1answer
18 views

Sum of child nodes in accounting hierarchy in django/python

How do you create a sum of child nodes into a a parent node for multiple levels in an accounting hierarchy with Python/Django? I currently have an app which displays the sum of individual accounts, ...
0
votes
2answers
43 views

Mergesort & recursion confusion/code not working

I've been at this for a couple days, reading many pseudocode and watching videos to explain recursion and mergesort. I understand mergesort and somewhat understand recursion -- except for when it ...
0
votes
3answers
40 views

racket: add a number to each element of nested list

I'm trying to write this function recursively. Please let me know if there's a library function for this in Racket documentation. Trying to add a number to every atomic element of a nested list. I'...
0
votes
1answer
36 views

How to build recursive slices of structs in Go?

I need to create a JSON payload from a slice of strings. They represent the path to an individual file. They need to be merged together into final JSON payload representing the whole directory ...
0
votes
2answers
42 views

Get all child and grand child of particular parent from given array

I have an array of all collections, I need to get child and grand child of given parent as array. [0] => Array ( [id] => 61 [name] => Fashion [slug] => fashion ...
7
votes
1answer
1k views

What are the differences in these SQL closure table examples?

I am having some difficulty wrapping my mind around SQL closure tables, and would like some assistance in understanding some of the examples I have found. Lets say I have a table called sample_items ...
5
votes
21answers
96k views

Creating a recursive method for Palindrome

I am trying to create a Palindrome program using recursion within Java but I am stuck, this is what I have so far: public static void main (String[] args){ System.out.println(isPalindrome("noon")); ...
1
vote
1answer
66 views

Printing all possible combinations of elements in an array

During recursion, once elements are changed in an array, this change persist. How to pass array so that changes are done according to the call stack ? Once element at index 2 is set, its set in every ...
2
votes
1answer
33 views

Ordered dictionary of ordered dictionaries need to be converted to dictionary of dictionaries

I have a dataset which might have n level of ordered dictionary of ordered dictionaries,Now i need to convert all of them into normal dictionaries,Is there a easier method to do other than recursive ...
-1
votes
2answers
39 views

Find first suitable array of elements

I have kind of a tree structure outputted from another method in a format: $tracks = [[1,4],[3,5],[2,3,5],[1,2],[2,4]]; Where top node is empty array and $tracks[0] = [1,4] combines first two nodes (...
-5
votes
0answers
37 views

Can you explain why it is giving the output 11 as ans [on hold]

Input: 38 Output: 2 Explanation: The process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. class Solution(object): def addDigits(self, num): """ ...