recursion

All Paths for a Sum with return issues

放肆的年华 提交于 2021-02-05 08:17:17
问题 I have a question in finding the all paths for a sum. The question is: Given a binary tree and a number ‘S’, find all paths from root-to-leaf such that the sum of all the node values of each path equals ‘S’. My approach with recursion is: def all_sum_path(root, target): result = [] find_sum_path(root, target, result, []) return result def find_sum_path(root, target, result, new_path): if not root: return None new_path.append(root.value) diff = target - root.value if not root.left and not root

Recursively add to strings in JS

落爺英雄遲暮 提交于 2021-02-05 08:14:12
问题 I'm tackling a recursion problem that returns a string of "hi"'s where the first "hi" has a capital H and the string ends with an exclamation point. I have the code below so far but I'm not sure how to prevent subsequent occurrences of "hi" having a capital H. Any guidance would be welcome. function greeting(n) { if (n === 0) { return ""; } else if (n === 1) { return "Hi!" } else { return `${'Hi' + greeting(n - 1)}` } } console.log(greeting(3)) // should return Hihihi! console.log(greeting(5)

Recursively add to strings in JS

扶醉桌前 提交于 2021-02-05 08:12:23
问题 I'm tackling a recursion problem that returns a string of "hi"'s where the first "hi" has a capital H and the string ends with an exclamation point. I have the code below so far but I'm not sure how to prevent subsequent occurrences of "hi" having a capital H. Any guidance would be welcome. function greeting(n) { if (n === 0) { return ""; } else if (n === 1) { return "Hi!" } else { return `${'Hi' + greeting(n - 1)}` } } console.log(greeting(3)) // should return Hihihi! console.log(greeting(5)

Create Javascript Object Having Nested Parent Objects

ⅰ亾dé卋堺 提交于 2021-02-05 07:57:27
问题 I am working on a Nodejs project. I have to create a function which takes an object (a child category) like: { id: 65, name: 'Outdoor', parent_id: 2 } Now I want my function to check for the parent category by using parent_id from database and return an array/object like this: { id: 2, name: 'Furniture', parent: { id: 1, name: 'Residential', parent: { id: ..., name: ..., parent: { and so on.. } } } } This is what I have done so far: * _get_category_parents(category, _array) { if(_array ===

XML XSD recursion and namespaces

℡╲_俬逩灬. 提交于 2021-02-05 07:44:47
问题 I'm trying to wrap my head around namespaces and recursion in XSD and I feel lost. Error: The QName value 'topic' does not resolve to a(n) element declaration Most important part is divided with whitelines, just learning xsd but I need it for one of my assignments, so please, take it easy... Code (I tried to follow this link: Recursive element in XML , unluckily no results): <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name='xmap-content'> <xs:complexType> <xs:sequence>

Functional/Stream programming for the graph problem “Reconstruct Itinerary”

[亡魂溺海] 提交于 2021-02-05 07:36:17
问题 I am trying to solve the reconstruct itinerary problem (https://leetcode.com/problems/reconstruct-itinerary/) in Scala using functional approach. Java solution works but Scala doesn't. One reason I found out was the hashmap is being updated and every iteration has the latest hashmap (even when popping from recursion) which is weird. Here is the solution in Java: import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map;

Flip Array Recursively

喜你入骨 提交于 2021-02-05 06:37:07
问题 There is this problem on LeetCode that I can not get to work in C/C++ The idea is to reverse an array in its place (using no other additional array) using recursion. The link is : https://leetcode.com/explore/learn/card/recursion-i/250/principle-of-recursion/1440/ The solution is done in Java or Python. I tried implementing the solution in C but I always get the original array, my code is as follows: void reverseString(char* s, int sSize){ if(!s) return; reverseString(s+1,sSize-1); s[sSize] =

Recursion, example of application of the recursion

雨燕双飞 提交于 2021-02-05 06:31:05
问题 let company = { sales: [{name: 'John', salary: 1000}, {name: 'Alice', salary: 1600 }], development: { sites: [{name: 'Peter', salary: 2000}, {name: 'Alex', salary: 1800 }], internals: [{name: 'Jack', salary: 1300}] } }; // The function to do the job function sumSalaries(department) { if (Array.isArray(department)) { // case (1) return department.reduce((prev, current) => prev + current.salary, 0); // sum the array } else { // case (2) let sum = 0; for (let subdep of Object.values(department))

recursion with a list in python

萝らか妹 提交于 2021-02-05 05:01:23
问题 I just started learning python and there are some recursion questions that I can't seem to figure out. The most annoying one is this: I need to build a function ind(e,L) where e is an int and L is a list. By entering e if it is in the list the output needs to be its index For example: ind(42,[0,14,52,42,15]) -> 3 This is the code I wrote this far but the index I get is always 0. Can someone please explain to me what I am doing wrong? def location(e,L): if L == []: return False elif e == L[0]:

recursion with a list in python

血红的双手。 提交于 2021-02-05 04:57:01
问题 I just started learning python and there are some recursion questions that I can't seem to figure out. The most annoying one is this: I need to build a function ind(e,L) where e is an int and L is a list. By entering e if it is in the list the output needs to be its index For example: ind(42,[0,14,52,42,15]) -> 3 This is the code I wrote this far but the index I get is always 0. Can someone please explain to me what I am doing wrong? def location(e,L): if L == []: return False elif e == L[0]: