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
0 answers
16 views

MATLAB Rounding only the finally returned value of a recursive function

I am trying to create a recursive FFT function which rounds its output to 1 decimal place, without rounding all the recursion outputs. My code produces the correct unrounded output when I do not ...
aknav's user avatar
  • 1
0 votes
1 answer
21 views

Kotlin reflection application to parsing, an example

I am experimenting with Kotlin's reflection mechanism. However, things go wrong when I try to apply the function recursively. I have been learning Kotlin for less than a month now and I am completely ...
ArekBulski's user avatar
  • 4,660
1 vote
1 answer
21 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 }...
Dark Rebellion's user avatar
-2 votes
3 answers
60 views

There is a lock of 4-digit PIN. The digits to be chosen from 0-5 for the keys. Find all possible combinations of keys

The problem goes like this: There is a lock of 4-digit PIN. The digits have to be chosen from the digits 0-5 for the key to unlock. We need to Find all possible combination like 0001, 0002, 0003, 0004,...
ABGR's user avatar
  • 4,755
0 votes
0 answers
52 views

Replace recursion in a randomized binary search tree with a loop?

I have a tree code using recursion: #include <iostream> #include <cstdlib> #include <ctime> #include <vector> #include <iterator> struct TreeNode { int value; ...
Gringo's user avatar
  • 1
-1 votes
0 answers
15 views

Implementing a Recursive Function to Generate Fibonacci Series in Python [duplicate]

I need assistance in designing a recursive Python function to generate the Fibonacci series up to a given number of terms. While attempting this, I faced challenges in optimizing the recursive ...
Sai Jayanth's user avatar
0 votes
0 answers
22 views

Print tree traversal path in Python

I have an n-ary tree. I want to print every node in a particular path, starting from the root node, and ending when the node possesses a certain quality. I’ve been using depth-first search. One by one,...
gruan's user avatar
  • 1
1 vote
1 answer
48 views

Is Recursion the Best Way to Solve My Candy Issues?

I've very new to programming and I've come up with a practice problem for myself that's proving very difficult for me. It's a lot of fun, but I could use some guidance! I don't have any actual code ...
csharp1321's user avatar
0 votes
1 answer
20 views

Validate API request attributes recursively

I am developing dynamic query builder for the admin users and for the same I am implementing POST API request to store all conditions together in database. I am stuck with the validation of POST json. ...
Avnee.Angel's user avatar
-1 votes
0 answers
37 views

Explain This code to me it's about closure recursion

package main import "fmt" func fact(n int) int { if n == 0 { return 1 } return n * fact(n-1) } func main() { fmt.Println(fact(7)) var fib func(n int) int ...
Ola Mamdouh's user avatar
0 votes
0 answers
23 views

insert function Binary Search Tree

So Basically i have made insert function in my BST @Override public TreeElement<T> insert(T value, Comparator<T> comp) { if(value == null) throw new ...
Dachacho's user avatar
0 votes
1 answer
37 views

Number of paths in a grid using backtracking

Given a square grid with size n, the goal is to find the number of paths from top-left to bottom-right going through all the squares in the grid. You are allowed to move in all four directions given ...
Tanmay Gejapati's user avatar
0 votes
3 answers
130 views

Javascript maximum call stack size even though recursive function call itself with an accumulator

I have an object whose value can be any type. const data = { a: { aa: 50, ab: 'hello', ac: 'xx_1' }, b: 50, c: [ { ca: 100, cb: 'by', cc: 'xx_2' }, { ca: 101, cb: 'by1', cc: '...
Loutag's user avatar
  • 21
-2 votes
2 answers
47 views

Recursively filter a multidimensional array using a 2d array of whitelisted key paths including wildcards

I have a multidimensional array with an indeterminant depth. I want to keep only some keys depending a collection of paths, but I need a generic, recursive function and not a hardcoded filter. Example:...
ReaperSoon's user avatar
0 votes
0 answers
20 views

Is it possible to write a recursive cte query to address data irregularities, where to continue the recursion it must match on different columns?

I have a large SQL dataset of features that have a series of start_nodes and end_nodes. I have written a recursive query to link and order these features by matching an end_node to start_node and ...
Brad's user avatar
  • 1
0 votes
1 answer
17 views

This program must raise the number two to a power that the user enters

Warning C6385 Reading invalid data from 'result'. 21 Warning C6386 Buffer overrun while writing to 'result'. 12 Warning C6385 Reading invalid data from 'number'. 58 When raised to a power ...
Ilya Ronzin's user avatar
0 votes
2 answers
45 views

Declaring foldl ( from left) in terms of foldr (from right)

I have foldl: let rec foldl f lst acc = match lst with | [] -> acc | hd::tl -> foldl f lst (f hd acc) and I have foldr: let rec foldr f lst acc = match lst with | [] -> acc | hd::tl ...
J.B's user avatar
  • 21
-1 votes
1 answer
40 views

Getting the two minimum values from a list in OCaml

I need to write a function twoMin: int list → int * int using foldl that returns the two smallest values (not elements) of a list. You may assume that the list contains at least two elements with ...
J.B's user avatar
  • 21
0 votes
0 answers
56 views

How to write 2 consequent recursions with Completable future?

I have the following code which tries to get a /ready endpoint of a service until it returns OK and then makes a call to that service when it is available: @PostConstruct void registerMetadata() { ...
TsB's user avatar
  • 1
-4 votes
0 answers
47 views

N Rooks problem with prefered Area for Rooks in C++ [closed]

I have an n*n Array with n Rooks i have to place in it. Each Rook has a preferred area where it has to be placed. Preference { int a // r1, c1 is the top left corner of the preffered area int b ...
x_salt_eater_x's user avatar
-1 votes
1 answer
25 views

Using foldl to get the mean of a list

I am trying to get the mean of a list by using the following helping function: let rec foldl f l b = match l with | [] -> b | x :: l -> foldl f l (f x b);; let avg l = foldl (fun x acc ->...
J.B's user avatar
  • 21
0 votes
1 answer
23 views

Append a list on to another in oCaml

I need a tail recursive function that appends a list1 infront of list2. I tried it this way let rec append lst1 lst2 acc = match lst1 with | [] -> acc @ lst2 | hd::tl -> append lst1 tl (...
J.B's user avatar
  • 21
0 votes
0 answers
20 views

how to use recursion to perform an exhaustive search for qwirkle board game moves?

I'm trying to implement a greedy bot algorithm for the game qwirkle. To those unfamiliar qwirkle is a tile based game. You always have a certain number of tiles on your hand. The greedy bot is ...
maximilian.gross1's user avatar
1 vote
1 answer
37 views

Trying to get the number of even and odd integers in a list

I am trying to write a recursive function that takes in a list as argument and returns the number of even and odd numbers in tuple (number_of_even, number_of_odd) let rec countEvenOdd lst e o = ...
J.B's user avatar
  • 21
1 vote
3 answers
32 views

OCaml list flattening using a recursive function

I am trying to get a recursive function to flatten lists where I am not allowed a helper function. I can only use @. And I'm trying to use "match with" I tried this way let rec flatten lst1 =...
J.B's user avatar
  • 21
0 votes
3 answers
99 views

how to search and loop array of object for particular key using recursion

I have a array of objects Some objects have id and some are not those having id can present with either "split" or "relation" key not both inside split there can be another split ...
Vikas Acharya's user avatar
0 votes
0 answers
15 views

How to parse an xml (.aml file [Automation Markup Language]) which has recurring xml tags in java?

I want to parse below xml. The catch is this xml is a very tiny portion of a very big file and the nested recurring InternalElement tag is not fixed i.e for some nodes there might be 3-4 levels , for ...
aditya ghumatkar's user avatar
4 votes
1 answer
92 views

Find all permutations of an array - Where am I going wrong?

So we have an array and I need to find all the permutations of the items in it. for e.g, arr = [1, 2] should return [[1, 2], [2,1]] Now here's my thought process: If the array is empty, it should ...
ABGR's user avatar
  • 4,755
0 votes
1 answer
42 views

Finding the runtime of a recursive 'Generating Parenthesis' algorithm

I wrote up a solution to this leetcode problem for generating different valid combinations of parenthesis. I'm having trouble understanding the runtime of my algorithm. Here's the code: class Solution:...
shyshy's user avatar
  • 1
0 votes
1 answer
61 views

Problem in thinking recursively. How to take recursive leap of faith?

The common guidelines I follow to solve problems recursively is do divide the problems in subproblems and solve for the smaller inputs. If it works, it would work for bigger inputs as well (Recursive ...
ABGR's user avatar
  • 4,755
0 votes
1 answer
41 views

OCaml higher order functions

Somebody please explain the algorithm in this problem for OCaml. I have the solution but I do not understand it. Define iterup: (int * 𝛼 → 𝛼) → int → int → 𝛼 → 𝛼. Iterup takes a function which ...
J.B's user avatar
  • 21
0 votes
0 answers
13 views

Implemented ArrayList not updating when I call my own Merge sort function

I have tried creating my own ArrayList class, and I am trying to implement a sorting function. I have implemented a recursive version of Merge sort as my sorting function, however when I call the ...
colezoom's user avatar
-2 votes
0 answers
58 views

recursionerror: maximum recursion depth exceeded in python 3.11 due to enum

I am having an enum class from enum import Enum class TestEnum(Enum): __test__ = False Value1 = 1 Value2 = 2 Value3 = 3 class_enum = TestEnum.Value1 The class_enum variable holds ...
Gokula Kannan's user avatar
0 votes
0 answers
16 views

Recursive least square algorithm estimation in system isentification

I have a y_tr = (1+2*tr.^2+4*tr.^4+6*tr.^6+7*tr.^7) and y_te = (1+2*te.^2+4*te.^4+6*te.^6+7*te.^7) y = θ1x1 + θ3x3 + θ5x5 + θ7x7 + θ8x8 t =1:1000 and t_tr is 700 and t_te is 300. I want the RLS ...
Seyed Ehsan Zolfaghari's user avatar
0 votes
0 answers
30 views

Breadth-first search in MySQL using a large amount of data

I have access to a MYSQL table in a mariadb server. The table 'ggLink' organizes information in the basic format of gene source and gene target as columns. We assume that every edge is unitary. Say ...
Yoyo458 A's user avatar
-2 votes
0 answers
26 views

remainder of subsequences (non-empty) of a number in string format [closed]

I came across this question in my assessment for a company where I was supposed to write a function which takes 4 inputs: N: integer in string format K: integer which divides the subsequences of N Q: ...
Preet Rathod's user avatar
1 vote
1 answer
69 views

Printing 2D array using recursion (C programming)

I'm currently practicing recursion so I can use it in the Tideman CS50 problem. Specifically, I'd like to learn to traverse a matrix (2D array) using recursion. For practice purposes, I've created a ...
ante's user avatar
  • 13
-1 votes
1 answer
40 views

Identifying pattern and code for this problem

I found this problem and I'm trying to come up with a solution for it, but I'm not getting the pattern here. Any advice on how to approach it? (I want to understand what the pattern is and a ...
Hazem El Sankari's user avatar
0 votes
0 answers
15 views

FamilyTree not printing descendats and Ancestorsm, just the name of the person. how can I fix that?

so I have to print descendants and Ancestor with indentation but when I run my code it only prints the name of the person. this is my code so far. import java.io.BufferedReader; import java.io....
Julian Martinez Reyes's user avatar
0 votes
2 answers
101 views

Tracing a recursive function

i have a homework assignment listed below Lee has discovered what he thinks is a clever recursive strategy for printing the elements in a sequence (string, tuple, or list). He reasons that he can get ...
Ian Schaak's user avatar
0 votes
1 answer
67 views

How do you point to a specific address in the heap memory? [closed]

So, I was doing my assignment and I'm quite unable to solve this problem. I am new to dynamic memory allocation and need help understanding it. So, heap memory is random. And if I use malloc, say in a ...
Ramoryx's user avatar
1 vote
1 answer
23 views

How can I set a value for each element of a Matrix of N dimension?

So, this is kinda long and complicated but hear me out, recently I´ve been studing about Matrices and setting coordinates to map the movement of NPCs in videogames and that got me into ...
Megalomaniacal Programmer's user avatar
0 votes
1 answer
66 views

Problem with recursive calls when x only takes t time, but when 10x it needs 100t time and much slower [closed]

I have an assignment about practicing the algorithm recursive method, and since I'm new about this, I wanted to ask about, if there's anything I could possibly do to improve the speed of my code here. ...
EintsWaveX's user avatar
-1 votes
4 answers
56 views

Syracuse sequence using recursive python functions

the problem is as follows, we get a positive integer, we keep modifying it until we get 1, if the integer is pair we divide by 2, if impair we multiply by 3 and add 1, i was able to solve this using a ...
Ghassan Hakim's user avatar
0 votes
2 answers
50 views

Recursive code works for string but not with lists when given as parameter

I am writing a recursive code to generate all the binary strings of length n with no consecutive 1s. Logic : Our recursive function works as follows: Check the last element in the array. If the last ...
Shashank Mingwal's user avatar
0 votes
3 answers
136 views

Instead of returning a list i get nil as a return type

I am new to elixir and functional programming as whole. I was starting out with super basic functions until i found a problem I couldnt find on the internet. I have the following module defmodule ...
mr.w1sh's user avatar
1 vote
4 answers
190 views

Time Complexity of the function

I got two versions of solving the same question. And I am really confused about how to calculate the time complexity (and chat gpt is confusing me even more lol). The first version: class Solution: ...
code_omelette's user avatar
0 votes
1 answer
64 views

Depth first search implementation: algorithm keeps on searching out only to the right

I have a Depth First Search exercise. The goal of this exercise is to find a valid path from the beginning of the maze to the end. This is my code: Node.java public class Node { private int ...
Root Groves's user avatar
0 votes
2 answers
75 views

Trouble understanding what's wrong with my recursion

My homework task: A web developer needs to know how to design a web page's size. So, given a specific rectangular web page’s area, your job by now is to design a rectangular web page, whose length L ...
AdHass's user avatar
  • 1
0 votes
2 answers
50 views

Reformatting a json tree using python

I have a json tree at hand, I want to reformat it. **The input is somewhat like : ** [ { "archive":{ "LOGS":{ "_files":[ &...
damd biker's user avatar

1
2 3 4 5
937