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.

0
votes
1answer
28 views

square the array element in O(n) running time

Is there a way to square the elements of the array with time complexity of O(n)? I tried two ways of doing it but I think they are both O(N^2) PS: I can't use "*", only addition/subtraction. 1. #...
-2
votes
0answers
22 views

Javascript: What are the space and time complexity differences between these 2 recursive Fibonacci functions?

Can someone explain to me the difference between these recursive fibonacci functions? Method 1: // Slow Fibonacci function fibonacci(n) { if (n < 2) { return n; } return ...
1
vote
0answers
15 views

recursive function python for DP

I have written in Python the following recursive function for computing a solution within a DP algorithm for a weighted interval scheduling problem where the intervals are "sorted_operations". OPT and ...
-2
votes
1answer
26 views

Missing operators in function recursively

I have a function I want to make, I have to found missing operators. For example, there is a list of [3,4,5,6] and I need to find by using operators an answer of 1. So it would be 3*4-5-6. (All ...
1
vote
1answer
12 views

Flatten lists in Prolog with additional clause

I'm currently starting to learn Prolog and struggling to solve even basic problems. My current assignment is to create a predicate that flattens a nested list. I'm not supposed to use the cut ...
1
vote
2answers
42 views

How to stop recursion in a CTE?

I have a database table which looks like this: ID | PredecessorID | Data -------------------------------------|--------------------------------...
-4
votes
0answers
23 views

How is Longest Palindromic Sequence is different from Longest Palindromic Subsequence? [on hold]

Can you plz tell me what are the things that make these two problems different to solve, i know what is the difference b/w the substring and subsequence. There dp solution and matrix looks very ...
1
vote
4answers
33 views

Recursion upside down triangle

I'm supposed to create a recursive statement that if first calls triangle(n) it returns '******\n *****\n ****\n ***\n **\n *' This above is called for triangle(6) and if I print(triangle(6)) it ...
0
votes
0answers
9 views

Single inverse one to one relationship in Realm (recursive object)

For a project I started with a model like this : A GrandFather has a Son, a Son has a Child... But I have to use Person instead. So a Person has a Person who has another Person... Or to be more ...
0
votes
0answers
41 views

Java Memoizing a recursive function for a some APP

I have a working recursive function for an assignment. However, part of the requirement is to get the complexity down by utilizing memoization. The function works similarly to a mobile tapping game. ...
0
votes
0answers
18 views

recursive nodejs mysql query

I want to execute a recursive function that retrieve data from DB. In php the code below run like a charm with 15ms to execute function GetSubCategories($catno,&$subcats, $useactive=true){ ...
0
votes
2answers
54 views

Convert tree to list of full paths in PHP

I am trying to convert a tree structure into a list of paths. The tree looks like this: array ( 0 => array ( 'name' => 'Default Category', 'children' => array ( 0 =&...
2
votes
2answers
68 views

C++ combination function always resulting 0

can anybody tell me why my Combination function is always resulting 0 ? I also tried to make it calculate the combination without the use of the permutation function but the factorial and still the ...
-3
votes
3answers
27 views

How to pass a list to a recursive function [Python2] [on hold]

I have a local list (inside a function) which I am passing to the same function's recursive call. I am modifying the list inside the recursive call but I don't want the changes to be reflected in the ...
0
votes
2answers
18 views

Printing queue.peek() in Recursion prints irrelevant values. I am trying to check whether linked list is palindrome using queue?

solve() function returns true if linked list is palindrome.Equating q.peek with Node value returns false even after values are equal. Tried printing q.peek() returns [email protected] I did ...
0
votes
2answers
22 views

Python (Recursion): “TypeError: string indices must be integers” in Recursion

Just started learning DS&A on my own recently and in the Recursion part of the book, it lists the code below as a basic example of Recursion being used to convert an integer into a string: def ...
0
votes
1answer
33 views

recursively get user input value in array values

I am leaning recursion and I want to create a search engine which depends on a user value and gets from an array all values which together make up the word that the user typed. For example I ...
1
vote
1answer
35 views

Recursive Selection Sorting (Java Eclipse Neon 2)

Okay okay. I have been working on a recursive selection sort in Java. I have done my reading, googling, stack overflowing, and am still unable to figure it out. I think the code is getting worse the ...
0
votes
2answers
46 views

Explanation of Recursive Function in Python

I am new to Python and trying to wrap my head around recursive functions. I am understanding the overall concept but I came across an example that I can't seem to understand fully what it is doing. A ...
0
votes
1answer
20 views

search string value in array values recursively?

new with recursive thing and i want to create search engine depend on value user typing and get it from array values all word in value that user typing for example i have this array : $array = array(...
0
votes
1answer
23 views

recursive pattern match with options

When you are patterning matching in OCaml and returning a type option, you give make your pattern return a Some or None. If your pattern returns the function (recursively), would you need to put Some ...
1
vote
4answers
66 views

Haskell:Check if first element in a list is a duplicate

I am writing a recursive function called threeN that takes a list with one element and adds a new number depending on if it's even or odd. If it's even divide it by 2, and if it's odd multiply it by ...
0
votes
1answer
22 views

Why is my method recursing when the only condition under which it recurses hasnt been meet?

In this program, I want getGuess to print an error message then rucurse if a condition is met (if the guess falls within a specific range), and if not, return the value of guess. When i run this ...
-2
votes
1answer
32 views

How do I turn an iterative STL List for loop function into a recursion?

So I have a simple for loop inside a class member function which prints the names of students a university wants to offer admission to. StudentPreferenceList and SchoolName are class member variables ...
-1
votes
1answer
78 views

Program causing stack overflow [duplicate]

I'm trying to solve this problem using recursion, but I seem to run into a stack overflow error beyond a certain divided that's not >= Integer.MAX_VALUE. Could anyone provide some insight into this ...
0
votes
0answers
51 views

Which is more efficient n^2 or n*lgn*lgn?

A problem that can be solved by a non-recursive algorithm in n^2 times. The same problem can be solved using a recursive algorithm in n lg(n) operations to divide the input into two equal pieces and ...
1
vote
0answers
45 views

(Scheme) Tail recursive modular exponentiation

I have an assignment to make a tail-recursive function that takes 3 integers(possibly very large), p q and r, and calculates the modulo of the division (p^q)/r. I figured out how to do make a function ...
0
votes
1answer
31 views

How to call a scanner recursively in this program? [duplicate]

This is my first post so i was unsure about formatting and didnt include the full program(i can if need be) I'm writing a program in which I want to use scanner in a method whose job is to receive a ...
1
vote
1answer
33 views

Data.frame of Data.frames

I'm using a data.frame that contains many data.frames. I'm trying to access these sub-data.frames within a loop. Within these loops, the names of the sub-data.frames are contained in a string variable....
0
votes
4answers
83 views

What is the correct way to make a list in Scala?

With a background from object-oriented programming I am not able to understand how to make immutable lists in Scala. Example; I want to make a list of 10 random people: object MyApplication extends ...
-1
votes
1answer
69 views

To what degree does this Haskell code satisfy this specification?

Listing 1 is a Haskell class signature and instance from a paper. The isIn person house function is supposed to return true if a person is in a given house or a given room. Listing 1 class ...
-2
votes
0answers
35 views

An algorithm to fill circles in a quadrilateral so that they don't go out of the circle and minimal space is left in the quadrilateral

You are a given a quadrilateral, you need to place circles in such a way that: 1)circles don't go out of the quadrilateral. 2)minimal amount of circles is needed. 3)minimal space in the quadrilateral ...
0
votes
2answers
45 views

Using recursion to determine the number of digits

I'm currently stuck on one line of code that I'm not fully understanding. So, I'm reading example codes from the book, and one of "programs" used recursion to determine the number of digits in an ...
-1
votes
4answers
34 views

Javascript not returning value

The following is an example of a very simple recursive function. As written, the code logs all the values >= to n, but it does NOT return the string value in the code block of the if statement. Can ...
-2
votes
0answers
23 views

i need change list in recursive money chnage

i need coin change result likes (minimum coin, counter, [coin change list]) if input=63 (3, XXXXX, [21, 21, 21] please complete 'make_change', here are main code and testcode def make_change(...
0
votes
1answer
30 views

Better representation of a recursive polymorhphic type structure

I am implementing a tree. During the execution of my program data gets added to the node. The code below shows my first implementation: type 'a tree = | Node of 'a | Leaf type nodata_node = { ...
-3
votes
0answers
23 views

Queens Placement [on hold]

I'm working on the 8 Queens Puzzle. Everything seemed to have been working fine. However now as I'm checking the code in the morning. The Queens aren't placing correctly. Can someone please look at ...
-1
votes
2answers
45 views

How to execute nested list first

Suppose I have a list with nested lists and a function for "NOT" and "OR", like so: def or_function(exp1, exp2): return exp1 or exp2 def not_function(exp): return not exp my_list = ["NOT", ...
0
votes
2answers
28 views

How do I implement a method that returns an inorder sorted array in a java binary search tree?

This is what I tried: public int[] sortedArray() { int[] array = new int[size()]; int index = 0; traverseInorder(root, array, index); return array; } private void traverseInorder(...
-1
votes
1answer
83 views

Returning the sum of all digits in int that are divisible by 2 c++ [on hold]

I need to sum all digits in an int that are divisible by 2 using recursion. This is what i have so far. If my int is "4544", when i run the code it prints out : 4 4 5 8 sum = 8 which is right at that ...
-1
votes
2answers
41 views

Counter Variable in Ocaml?

If you can't use mutable states in Ocaml, How would you go about making a counter variable in a recursive function. For example: Say you have a data type e you want to pattern match an expression (...
-5
votes
0answers
24 views

Calculating value of pi with continued fraction using recursion in Python [on hold]

How look like a recursive function is python which is evaluating the value of pi using continued fractions?
2
votes
1answer
68 views

How do I recursively de-select cousin nodes when selecting sibling nodes (recursively rendered checkboxes)

I am building a multi-select checkdown group item list. The goal is to have only ONE active group at a time. So the user can select parents - but if they select a child item, that child group becomes ...
0
votes
2answers
42 views

Recursive algorithm to establish level of pairs element or to find the master element

I am having a hard time trying to solve the next problem. I have a List of object (in java) which stores data in fields like: Master Dependent 100 101 101 102 105 107 111 112 110 111 ...
0
votes
2answers
38 views

Recursive call in php over a json

I have this json data $innerdata = json_decode('{ "sync_block": false, "contacts": [{ "con_title": "", "con_fName": "", "...
0
votes
1answer
94 views

Memoizing a recursive function for a game algorithm?

I have a a working recursive function for an assignment. However, part of the requirement is to get the complexity down by utilizing memoization. The function works similarly to a mobile tapping game....
0
votes
0answers
13 views

Syntax error in Union All of recursive CTE postgresql

I have been struggling to fit in my logic into recursive cte as it seems its the best approach to solve the hierarchization problem using SQL. On running a this structure below I am getting syntax ...
0
votes
1answer
19 views

recursive function for Employee Heirarchy (nodejs)

I have data in table in this format emp_id,emp_name,title,supervisor_id,supervisor_name 11,Anant,Business Unit Executive,8,abc 15,Raina,Analysis Manager Senior,11,Anant 16,Kumar,Conversion Manager,...
0
votes
3answers
63 views

How can I walk through this recursive binary tree problem?

This is a working solution of the problem: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { ...
1
vote
0answers
18 views

Understanding the recursive build of trees(data.tree) in R

I am trying to build trees recursively in R using data.tree package.Anyone who is answering please understand recursion below.The code below contains only two levels of recursion.In the second level ...