recursion

Recursion flow in c language and how the output is printed

こ雲淡風輕ζ 提交于 2021-01-29 06:36:55
问题 How is the value of I getting printed? I know the recursion calls itself again and again. According to me, the function should return empty as the function is called before printing. How is the printf working? recur(int i) { if(i<0) return 0; recur(--i); printf("%d",i); recur(--i); } main() { recur(2); } The output of this program is -1 0 1 -1 Can someone explain how is it working? 回答1: To understand what happens, you have to understand how recursion works. Every recursive function requires a

having trouble understanding this code

≡放荡痞女 提交于 2021-01-29 05:45:34
问题 I just started learning recursion and I have an assignment to write a program that tells the nesting depth of a list. Well, I browsed around and found working code to do this, but I'm still having trouble understanding how it works. Here's the code: def depth(L) : nesting = [] for c in L: if type(c) == type(nesting) : nesting.append(depth(c)) if len(nesting) > 0: return 1 + max(nesting) return 1 So naturally, I start to get confused at the line with the append that calls recursion. Does

All possible paths from top left to bottom right of a mXn matrix

久未见 提交于 2021-01-29 05:43:33
问题 I was going through this leetcode problem for going from top left to bottom right. How many possible unique paths are there? I was able to understand this method of dynamic programming by storing the result for every index. public int uniquePaths(int m, int n) { int count[][] = new int[m][n]; for (int i = 0; i < m; i++) count[i][0] = 1; for (int j = 0; j < n; j++) count[0][j] = 1; for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { count[i][j] = count[i-1][j] + count[i][j-1]; //+

Recursive function that returns combinations of size n chosen from list

我与影子孤独终老i 提交于 2021-01-29 05:13:37
问题 I am trying to write a recursive function which takes as its inputs an integer n , and a list l , and returns a list of all the combinations of size n that can be chosen from the elements in l . I'm aware that I can just use itertools, but I want to become better at writing recursive functions, and I believe writing my own function will help me. So for example, if you input: n = 3 l = [1, 2, 3, 4] I want the output to be: `[ [1, 2, 3], [1, 3, 4], [2, 3, 4], [1, 2, 4] ] So far I've written

Maze generation - recursive division (how it works?)

房东的猫 提交于 2021-01-29 04:50:24
问题 I would like to learn how to generate a perfect maze. I am trying to understand Recursive Division Algorithm. I can't understand how to implement this algorithm. This is my code: bool maze[20][20]; void initMaze() { for(int i = 0; i < 20; i++) for(int j = 0; j < 20; j++) maze[i][j] = false; for(int i = 0; i < 20; i++) maze[0][i] = true; for(int i = 0; i < 20; i++) maze[19][i] = true; for(int i = 0; i < 20; i++) maze[i][0] = true; for(int i = 0; i < 20; i++) maze[i][19] = true; } int randNum

Create a Hosoya's Triangle in java

蹲街弑〆低调 提交于 2021-01-29 04:15:00
问题 So I'm trying to create a Hosoya's Triangle in Java, and I'm running into some math issues. I have started by making an empty 2D array that has a user input # of levels: int[][] tri = new int[levels][]; //build the empty triangle for (int row =0;row< tri.length;row++){ tri[row] = new int[row+1]; } this part is functional. The problem I'm having is in the next part, where I try to fill the triangle with Fibonacci numbers (I add the previous chunk of code because I'm thinking maybe building

recursive binary search in ruby

只谈情不闲聊 提交于 2021-01-29 03:52:48
问题 I've been learning some algorithms and I can't find the reason why my method is failing. if you could look at the code and shed some light as to why that is happening. I would truly appreciate it. I'm trying to write a method that would binary search an array recursively and so far that is all my code. def recursive_binary_search(arr, target) max_index = arr.length - 1 mid_index = max_index / 2 if arr[mid_index] > target new_arr = arr[0..(mid_index - 1)] recursive_binary_search(new_arr,

What happens when recursion is called twice in a method?

梦想的初衷 提交于 2021-01-29 03:07:33
问题 I'm pretty new to programming and I have now come to the concept of recursion. I have solved a few basic assignments but when I come to multible recursion I get terribly lost. I have tried to solve the following recursion several times but just cant get it right. A recursive method is called with the arguments "ABCD" and 2 , i.e. recMethod("ABCD", 2); . public void recMethod( String str, int n ) { if( n >= 0 ) { recMethod(str, n – 1); System.out.print(str.charAt(n)); recMethod(str, n – 1); }

I saw this recursion template in XSLT and cannot understand what is happening

只谈情不闲聊 提交于 2021-01-29 03:07:05
问题 What happens when the control gets into xsl:variable? It should never reach <xsl:value-of select="$number * $recursive_result"/> as it is calling the template again and again but it does. This made me question the overall control flow of XSLT. Please explain! <xsl:template name="factorial"> <xsl:param name="number" select="1"/> <xsl:choose> <xsl:when test="$number < 1"> <xsl:value-of select="1"/> </xsl:when> <xsl:otherwise> <xsl:variable name="recursive_result"> <xsl:call-template name=

How to stop the recursion in prolog, once the desired value is returned?

若如初见. 提交于 2021-01-28 19:51:02
问题 The .pl file that I am consulting looks like this spouse(eddard_stark,catelyn_stark). spouse(X,Y):-spouse(Y,X). What I fundamentally wanted program here was that if 'Eddard is spouse of Catelyn' then 'Catelyn is spouse of Eddard'. But when I query spouse(eddard_stark, X). this goes into an endless recursive return of catelyn_stark . I am not sure how to stop recursion in Prolog once desired output is reached. Also if you think of any alternate solution for this problem please mention it, I