# 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.

33,858
questions

**0**

votes

**1**answer

8 views

### Powershell - Return file path and context text line after recursive search

Trying to do a recursive search through all .txt files for a specific word and return the path of the file where the text was found and the context text line.
Currently using the below PowerShell ...

**0**

votes

**1**answer

36 views

### How do you write multiple lines in a .txt with recursion?

Im a programming student and the Engineer told us to write an algorithm using recursion for the math problem "Hanoi Towers". That is done, Im able to print the instructions on the console, but I need ...

**0**

votes

**1**answer

18 views

### Divide 2 integers (leetcode 29) - recursion issue

I'm having issues when i need to do a count (or aggregation) in recursion problems, for eg. in this problem where i'm trying to divide 2 integers w/o using multiplication, division and mod operator.
...

**1**

vote

**0**answers

21 views

### Mongoose: Need .pre hook to recursively delete children in tree

I have defined a schema that makes up a recursive tree structure in Mongoose that works quite well. Each node has an array of children that refer to child nodes in the same tree and schema. The tree ...

**2**

votes

**3**answers

58 views

### When is it ok to increase the max stack size of the jvm (-Xss)?

What am I doing ?
I am doing a course on coursera which requires me to write a program that calculates the height of a tree. The input is a parent array where each index is the node and its value is ...

**0**

votes

**1**answer

15 views

### Is this function tail recursive or not?

The sum_aux function is (obviously) a tail recursive function. Is sum also a tail recursive function? It does nothing more than call sum_aux once.
Otherwise, I understand why sum_aux is tail ...

**2**

votes

**3**answers

92 views

### Unflatten a flat array in Scala

I have a flat array like this and another flat array that describes the dimensions:
val elems = Array(0,1,2,3)
val dimensions = Array(2,2)
So now I must be able to unflatten that and return a 2*2 ...

**1**

vote

**1**answer

26 views

### Recursive function returning empty string

How do I properly exit a recursive function and return a value in VBA?
I have this simple code to remove multiple spaces from a string:
Public Function RemoveMultipleSpaces(s As String) As String
...

**0**

votes

**0**answers

30 views

### How to get proper debug context from production PHP code? print_r vs var_export vs var_dump corner cases

Requirements
We have production PHP code running on a webserver. In some sitations we want to enrich debug output (going e.g. to error.log) with context information. The context can have arbitrary ...

**1**

vote

**1**answer

40 views

### Recursively replace string using a CTE

I am having lookup_table as following:-
| id | Val |
+------+-----+
| 1 | A |
| 11 | B |
| 111 | C |
| 1111 | D |
I am creating words using the values from lookup_table like $id! and ...

**0**

votes

**1**answer

31 views

### recursion with or without return statement [duplicate]

this is the problem I solved for recursion
phone_book return
[119, 97674223, 1195524421] false
[123,456,789] true
[12,123,1235,567,88] false
when it have the prefix of the any number ...

**1**

vote

**1**answer

48 views

### Recursively Pass Template Template To a Template Template Function

It seems to me, that since in C++ we can pass a template template, then a function accepting a template template should be able to pass a (its) template template into itself. However this appears not ...

**3**

votes

**3**answers

61 views

### Array.reduce and recursion

I am trying to nest a recursive function in an Array.reduce call.
But the following does not work for the third instance only
const i1 = {'local': [['a','b']]};
const i2 = {'local': [['c','d']], '...

**3**

votes

**1**answer

24 views

### How can I immediately break from searching through directory recursively once desired file is found?

I'm recursively searching through a directory structure for a file with a specific name. I want to break as soon as the first instance of that file is found. How can I accomplish this? Currently it's ...

**2**

votes

**1**answer

43 views

### How to cbind all combinations of rows of a list of matrices in R

I have a list of matrices. The length of this list is unknown (although for my example I use a list of 3 2x2 matrices).
[[1]]
[,1] [,2]
[1,] 1 1
[2,] 2 2
[[2]]
[,1] [,2]
[1,] ...

**0**

votes

**1**answer

33 views

### List directories and sub-directories and store every level in array

Want to scandir the root folder first, store the found directories in array (level 1).
Then scandir each sub-directory that I've found and store them inside level 1, as level 2 and so on.
How can I ...

**0**

votes

**2**answers

70 views

### Building a string in Clojure with recursion

I need to generate a random char and build a string and only stop when my string contains the newly generated char.
(defn rand-char [len]
(apply str (take len (repeatedly #(char (+ (rand 26) 65)))))...

**0**

votes

**1**answer

40 views

### Summing recursive function to itself in Inclusion Exclusion Principle

This code is for Inclusion-Exclusion Principle to get numbers that are divisible by 2, 3, 5, 7 and it works fine.
public class Inclusion_Exclusion_Recursion {
private static int inc_exc(int index , ...

**1**

vote

**2**answers

34 views

### Recursion implementation of LeetCode Problem

Problem Name : 413. Arithmetic Slices
Problem Statement :
A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive ...

**0**

votes

**1**answer

69 views

### Stack overflow due to deep recursion

I have an assignment to solve the coin change problem which goes like this:
i have 5 types of coins {1,5,10,25,50}, We want to make changes with these coins for a given amount of money, in how many ...

**0**

votes

**1**answer

36 views

### How to recursively collect linked information in nested list on Python?

i am a beginner on python and I have a problematic situation that I don't know how to handle.
I have a nested list which have a string and a tuple of three string for each sublist:
[['V1', ('P1', 'B'...

**0**

votes

**1**answer

20 views

### Upload plugin folder in to wordpress directory in google conpute engine VM instance from my PC

I was searching everywhere and couldnt able to find out how to upload a folder to my wordpress directory in copute engine VM. I just want to upload a modified plugin to my wp plugin directory in ...

**1**

vote

**0**answers

39 views

### How to recursively call a closure that is stored in an Arc<Mutex<_>>?

I’m trying to transpile a dynamic language into Rust and closures are the most difficult part to implement.
I've tried using a Arc<Mutex<dyn FnMut>>, but it doesn't support recursion.
...

**0**

votes

**1**answer

64 views

### Optimising recusive backtrack

I solved a variation of the knapsack problem by backtracking all of the possible solutions. Basically 0 means that item is not in the backpack, 1 means that the item is in the backpack. Cost is the ...

**-1**

votes

**0**answers

90 views

### Can't understand where the error in the algorithm

I solved the puzzle. Its conditions are as follows.
Input: line, its length is from 1 to 8 digits.
At the output: the original line and possible line options taking into account vertical and ...

**0**

votes

**0**answers

60 views

### Too much recursion when trying to return field value

I'm working on Alpaca form with some Javascript and made compute-function to calculate whether endDate is equal or greater than startDate, then keep the value and if not, then empty the endDate.
I ...

**0**

votes

**1**answer

30 views

### New to Python and recursion - XML parsing

I can open and read an XML file.
What I'd like to do is a recursion to print out all child elements of the current node.
The (awful) iterative approach has nested for loops:
root = tree.getroot()
...

**0**

votes

**4**answers

54 views

### How to break out of recursive function?

I have a recursive function where I'm trying to flatten a list of nested lists. I want to stop the recursive function as soon as I find an integer. An example list would be: [[[1.2, 2.3, 3.3], [4.3, [[...

**-1**

votes

**2**answers

94 views

### Finding which matrix row has the highest sum java

I'm trying to write a recursive non-static method that for each given matrix will return the row number that has the highest total sum. I am not allowed to use static method and for loops (in any ...

**1**

vote

**1**answer

64 views

### Order of execution in a recursive call

When a return command has two recursive calls such as
return fib(n-1) + fib(n-2);, are both calls executed at the same time, or is fib(n-1) executed before fib(n-2)?
By using memoization the time ...

**-1**

votes

**0**answers

55 views

### Can't understand python function [on hold]

I am trying to study for an exam and in one of the practice questions we are given to look at we are asked to determine the output of a recursive function:
def mystery(lis):
if len(lis) == 0:
...

**2**

votes

**1**answer

79 views

### Looping through all values of a byte array

I'm trying to loop through all values of a small byte array (64 bytes). I want to increment the way a digital clock would, start at index 0, go 0-255 then increment index 1, repeat. I know this is ...

**0**

votes

**1**answer

32 views

### Nesting Code in Function Generates Recursion Error

I'm trying to parallelize my code that simulates data. On it's own, the code runs beautifully. However, when I nest this code within a function, and call the function, I get the Recursion Error.
I ...

**0**

votes

**1**answer

94 views

### Recursive function inside a simple loop time complexity

Why is it that inside a for loop and calling a recursive function results to the time complexity of O(2^N) not O(N 2^N) of this code below. Basing on the book CTCI.
void allFib(int n){
for (int i ...

**0**

votes

**0**answers

37 views

### Parsing jsonLogic into tree

I'm trying to recursively parse jsonLogic into a rule tree. By this, I mean, the input is some rule i.e.:
"if": [
{ "==": [{ "var": "code" }, "fruit"] },
{
"if"...

**-1**

votes

**1**answer

40 views

### Passing an argument into a nested function that has been operated on

I'm trying to iterate on the values x1,y1,x2,y2 by first, adding 16 to some x1 and x2 and then taking those values that have already been transformed under the while loop and operating on them again ...

**1**

vote

**2**answers

70 views

### How to transpose an m*n matrix using recursion?

I'm trying to transpose a matrix using recursion. Now, I know that under normal circumstances this isn't a good idea and a nested loop/nested map, or a similar approach is superior, but I need to ...

**0**

votes

**1**answer

82 views

### Why does this recursive javascript function expression return undefined?

I wrote a solution to this standard everyday homework problem to solve Pascal's Triangle recursively. Console.log shows the answer my function derived. Printing to innerHTML of a div writes the ...

**2**

votes

**2**answers

81 views

### How does this function print strings in the reverse order?

I am learning about strings and characters in c programming.Today i saw a code that print strings in the reverse order but i don't understand how it works.Can you help me to understand it ?
void ...

**3**

votes

**2**answers

26 views

### Control flow in recursion python

I created the following function:
def test_recursion(x):
print(x)
while x < 10:
test_recursion(x+1)
test_recursion(x+2)
print("end of function reached")
test_recursion(...

**-1**

votes

**2**answers

62 views

### Generating all letter combinations but cannot append to list [on hold]

I'm solving a leetcode problem 17. Letter Combinations of a Phone Number, I've successfully generated all the combinations as I can see using my print statement, however, I need to add these ...

**0**

votes

**0**answers

55 views

### Remove repeating numbers from for loop

I have the following code in R (published by Mark - Finding all possible combinations of numbers to reach a given sum) that allows me to add numbers upto the target value.
However, I want the code to ...

**0**

votes

**1**answer

48 views

### Transform multiple non-primitive recursive calls to iterative solution

I am writing a handwritten recursive descent parser as a self exercise. I'd like to see if an iterative approach is possible. Generally speaking, I'd like to know what mindset I should have in order ...

**2**

votes

**1**answer

90 views

### How to implement simple recursive filter using HTML buttons

I want to add recursive filters using simple HTML buttons/javascript. Till now, i have only been able to add one level of filter. What i want to do is - allow the user to select the filter in 2 stages....

**3**

votes

**1**answer

46 views

### Node.js / Javascript multiple recursive promise to return at view

I currently have an algorithm with multiple recursive calls from which I expect everyone to return in order to consolidate my result
The problem is that there are so many recursive calls that I no ...

**0**

votes

**0**answers

15 views

### SQLAlchemy basic recursive CTE example - hierarchical tree query

I am having trouble understanding how recursive CTEs are to implement in SQLAlchemy. Either there are no examples, I dont understand them or could not find them.
I am actually using Django 2.2 with ...

**4**

votes

**4**answers

90 views

### Recursion order of outputs in C

I have this recursive function in C,and i'm trying to understand why it's printing the way it does:
void rec(int x, int y){
if (x == y)
return;
rec(x--, y+1);
printf("%3d%6.2f\n", x, (float) ...

**-1**

votes

**2**answers

41 views

### How to convert BigQuery Struct Schema string to Javascript object?

I have extracted the schema of my BigQuery table from the "INFORMATION_SCHEMA" table. I get the list of all the columns in the table in a proper Javascript Object format except of the "Struct" and "...

**1**

vote

**3**answers

102 views

### Recursively finding minimum value (no built-ins or loops)

Below is my code for finding the minimum integer in a nested list. It throws this error:
TypeError: '<' not supported between instances of 'int' and 'list'
And I know why, but cannot figure out ...

**1**

vote

**1**answer

58 views

### reducing nested dictionaries defined by a boolean operator

Suppose I have a payload like the following:
payload = {
"OR": [
{
"AND" : [[1,2,3],[3,4]]
}, # ([1,2,3] AND [3,4]) --> [3]
{
"OR" : [
...