recursion

Python recursion and return

自闭症网瘾萝莉.ら 提交于 2019-12-24 05:23:13
问题 I'm new to the concept of recursion, had never practiced this magic in my coding experience. Something I'm really confused about Python recursion is the use of "return". To be more specific, I don't quite understand when to use return in some situations. I've seen cases where the return is used before recursion, and cases return is not needed at all. For example: A Leetcode Question: "Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the

Mutual Recursion Question

梦想与她 提交于 2019-12-24 05:12:30
问题 How do I change two functions that are Mutual Recursive to each other to make them into a linear recursion? Do I have to have both the methods in a single method? 回答1: You should be able to simply "inline" the implementation of the second method, into the first method. That is, public static void methA() { // snippet 1 methB(); // snippet 2 } public static void methB() { // snippet 3 methA(); // snippet 4 } becomes public static void methAB() { // snippet 1 // snippet 3 methAB(); // snippet 2

Mutual Recursion Question

▼魔方 西西 提交于 2019-12-24 05:12:14
问题 How do I change two functions that are Mutual Recursive to each other to make them into a linear recursion? Do I have to have both the methods in a single method? 回答1: You should be able to simply "inline" the implementation of the second method, into the first method. That is, public static void methA() { // snippet 1 methB(); // snippet 2 } public static void methB() { // snippet 3 methA(); // snippet 4 } becomes public static void methAB() { // snippet 1 // snippet 3 methAB(); // snippet 2

How to convert recursive procedure with side effects on ref param to recursive function returning a list?

夙愿已清 提交于 2019-12-24 05:08:09
问题 It seems every time I go to write a recursive function I end up making it return void and using a ref parameter. I'd much rather be able to write a function that just returns a result list. Apologies if the answer is very simple - for some reason it elludes me. Here's the code I have now: public static void GetResrouces(string startURL, ref List<XDocument> result) { var doc = XDocument.Parse(GetXml(startURL)); // GetXml ommitted - returns xml string var xs = new XmlSerializer(typeof

Recursive SQL Server query

北城余情 提交于 2019-12-24 04:56:07
问题 In a table reviewers with a structure like this: reviewer | reviewee =================== 2 | 1 3 | 2 4 | 3 5 | 4 In a function call, I know both a reviewer-id and a reviewee-id (the owner of the item the reviewee is looking to retrieve). I'm now trying to send a query that iterates all the entries in the reviewers table, starting with the reviewer, and ends at the reviewee's id (and matches that to the reviewee id I know). So I'm trying to find out if there is a connection between reviewee

How to calculate the highest score when taking a path through a grid?

时间秒杀一切 提交于 2019-12-24 04:49:19
问题 We have a 3x3 grid with the following values: 1 3 4 7 2 1 4 1 8 A person starts on the leftmost column and can then move either northeast, east or southeast. Now I have to find the optimal path through this grid. The starting point can be anywhere on the leftmost column. In this case the answer was 17 because the optimal path was 7->2->8. I would like to know how to compute this optimal path and how to compute it for larger grids. Thanks! 回答1: You can solve this problem with a bottom-up

Invalid Result in Prolog Program

只愿长相守 提交于 2019-12-24 04:44:06
问题 My program is intended to get rid of repeating elements in a list. I think its correct. I tried to make it work by putting cuts, but it didn't give correct results. member(X,[X|_]). % If X is Head, it is a member member(X,[_|T]) :- member(X,T). % If not, Recursively check the tail. remove_duplicates([],[]). remove_duplicates([H|T],R):-member(H,T),remove_duplicates(T,R). remove_duplicates([H|T],[R|REM]):-remove_duplicates(T,REM). However I am getting results: I = [_G2608, _G2611, _G2614|_G2615

Binary Tree Traversal Sum Of Each Depth

南笙酒味 提交于 2019-12-24 04:28:08
问题 I am looking for an algorithm or modification of an efficient way to get the sum of run through of a tree depth, for example: Z / \ / \ / \ / \ X Y / \ / \ / \ / \ A B C D The number of final leaves is four so us have four final sums and they would be this respectively. [Z+X+A] [Z+X+B] [Z+Y+C] [Z+Y+D] If someone could guide me in the right direction into getting the sums of all possible depths, that would be great. This will be done in python with fairly large trees. 回答1: You can recurse over

LISP: multi-level recursive reverse function

霸气de小男生 提交于 2019-12-24 04:26:13
问题 How to reverse a list such that every sublist is also reversed? This is what I have so far: (defun REV (L) (cond ((null L) nil) ((listp L) (append (REV (cdr L)) (list (car L)))) (t (append (REV (cdr L)) (list (car L)))))) 回答1: You are on the right track, but your last two conditions have the same action, which should give an indication that one of them is not doing what it should. Indeed, the second condition, the listp case, is not right, because when it's a list, you need to append the

Recursion in Prolog - Finding Path Between Cities

一个人想着一个人 提交于 2019-12-24 04:15:07
问题 I'm trying to work my way through the exercises at the bottom of this page and I find myself utterly confused on number 3. We are given the following knowledge base of travel information: byCar(auckland, hamilton). byCar(hamilton, raglan). byCar(valmont, saarbruecken). byCar(valmont, metz). byTrain(metz, frankfurt). byTrain(saarbruecken, frankfurt). byTrain(metz, paris). byTrain(saarbruecken, paris). byPlane(frankfurt, bangkok). byPlane(frankfurt, singapore). byPlane(paris, losAngeles).