recursion

Recursive promises to create tree

痴心易碎 提交于 2020-01-07 05:28:07
问题 A tree api returns childrens when node element is passed to it. I pass the root node first and then on the basis of the nodes returned I pass all of them back recursively, if they have hasChildren parameter true. Is there a way to know when the function has finished creating the tree. function recursivelyFetchChildren(id, selectedAsset, parentId){ return ajaxCall(id, selectedAsset, parentId) .then(function(data){ //collects all childs in childs array childs.push(data); for(var i=0; i<data

Find a list of related classes Recursively

谁说我不能喝 提交于 2020-01-07 04:45:12
问题 Supose I have a class representing a friend and another one representing pairs of best friends and a list of pairs of best friends and want to know each friend that is indirectly related to each one's best friend, as follows: class Friend { string Name; } class BestFriends { Friend NameFriend1; Friend NameFriend2; } List<BestFriends> ListOfBestFriends = new List<BestFriends>(); Supose I have the BestFriends pairs as follows: Adam and Brian; Brian and Chris; Chris and Daniel; Eddie and Ian;

Recursion to Iteration- refactoring

若如初见. 提交于 2020-01-07 03:50:30
问题 I wrote this code but now I want to refactor it in order not to use recursion. But I can't wrap my head around it? Any ideas guys? public List<ServiceDTO> findCustomerServices(String customerId) { List<Service> serviceTree = contractService.findCustomerServices(customerId); List<ServiceDTO> serviceDTOs = new ArrayList<ServiceDTO>(); cloneTree(serviceTree, serviceDTOs); return serviceDTOs; } private void cloneTree(List<Service> services, List<ServiceDTO> clonedServices) { for (Service service

PYTHON Return a list from a recursive function

假如想象 提交于 2020-01-07 03:45:09
问题 I'm coding a program, a part of the program is that I want to create a list with all the substring from a string, using a recursive function. However, when I return the list, I get nothing. The variable substringList has None value. How can I return the list, without losing all the data in it? def main(string): substringList = [] substringList = substring(string, substringList) def substring(string, substringList):#Recursive function to create all the length = len(string) #substrings**strong

How to preserve order of children to appear after their parents

不打扰是莪最后的温柔 提交于 2020-01-07 03:16:08
问题 Expected order by replyid: 55, 57, 58, 59, 60, 56 -- So that the entire 1st parent reply and all its children appear BEFORE the 2nd parent reply The following SQL query returns the wrong order of results WITH RECURSIVE t(replyid, replypid, depth, path, reply, replied, reply_userid) AS ( (SELECT replyid, replypid, 0, array[replyid], reply, replied, replies.userid, u.displayname, u.email_address, (SELECT COUNT(*) FROM reply_revs WHERE replyid = replies.replyid) AS reply_revs FROM replies LEFT

Parallelizing recursive function using OpenMP in C++

≡放荡痞女 提交于 2020-01-07 02:04:08
问题 I have the following recursive program which I would like to parallelize using OpenMP: #include <iostream> #include <cmath> #include <numeric> #include <vector> #include <algorithm> #include <thread> #include <omp.h> // Determines if a point of dimension point.size() is within the sphere bool isPointWithinSphere(std::vector<int> point, const double &radius) { // Since we know that the sphere is centered at the origin, we can simply // find the euclidean distance (square root of the sum of

Parallelizing recursive function using OpenMP in C++

怎甘沉沦 提交于 2020-01-07 02:02:39
问题 I have the following recursive program which I would like to parallelize using OpenMP: #include <iostream> #include <cmath> #include <numeric> #include <vector> #include <algorithm> #include <thread> #include <omp.h> // Determines if a point of dimension point.size() is within the sphere bool isPointWithinSphere(std::vector<int> point, const double &radius) { // Since we know that the sphere is centered at the origin, we can simply // find the euclidean distance (square root of the sum of

How to store values in a vector with nested functions

♀尐吖头ヾ 提交于 2020-01-06 21:07:15
问题 Following with this question R: from a vector, list all subsets of elements so their sum just passes a value I´m trying to find a way to store the values given by the print command in a vector or matrix but I can´t find any way to do it. The code is: v<-c(1,2,3) threshold <- 3 # My threshold value recursive.subset <-function(x, index, current, threshold, result){ for (i in index:length(x)){ if (current + x[i] >= threshold){ print(sum(c(result,x[i]))) } else { recursive.subset(x, i + 1,

Convert sorted array to binary search tree with minimal height

早过忘川 提交于 2020-01-06 19:50:36
问题 I want to convert a sorted integer array into a binary search tree. I have posted my code below. What I cannot picture is how the recursion actually works with the for loop as inserting. So if my array is [1,3,4, 5,8,10] I make 4, which is the mid of the array, become the root of my BST, then loop from the start of array and insert to the tree with root just created. My question is why the order of result inserted is not as the sorted given array? public TreeNode sortedArrayToBST(int[] A) {

Logic on a recursive method

时光毁灭记忆、已成空白 提交于 2020-01-06 19:35:35
问题 One of my exercises requires me to write a recursive method in which a list is given, and it returns the same list with only every other element on it. for example : List {"a", "b", "c"} would return List{"a","c"} I am writing in scala, and I understand that it has built in library but I am not supposed to use those. I can only use if/else, helper methods,and patterns. How could I parse thru a list using head and tail only? so far I have this: def removeLetter(list:List[String]):List[String]