python - Recursion not breaking -


I am trying to solve the Euler problem where I have to find the maximum to top to bottom. I'm trying to use recycling, but I'm left with it.

I think I did not describe my problem before, I'm trying to get it by recycling, to find the sum of the maximum number path. I start from the top of the triangle, and then checking this condition is 7 + upsum () big or 4 + upsum () is big Findsum () It is considered to find the number of numbers below I ' The result is the amount of money being deposited in the problem that I do not know the breaking case of this recursion function. I know that when it has reached the elements of the child, it should be broken, but I do not know how to write this argument in the program.

  pyramid = [[0,0,0,3, 0,0,0,], [0,0,7,0,4,0,0], [0,2] , 0,4,0,6,0], [8,0,5,0,9, 0,3]] pos = [0,3] DEF downloaddown (pyramid, pause): # left child to come back Try: Return (Pyramid [0] +1] [P [1] -1]: Excluding: Returns (0) Deaf Tireless (Pyramid, Pause): Try to return the correct child: Returns (Pyramid Excluding [Pos ([0] +1] [Pause [1] + 1]): Returns (0) Result = 0 Df Search_max (Pyramid, Pause): Global results if downlaidum (pyramid, pause) + search_max (pyrami (Pyramid, pause) + search_max (pyramid, [pause [0] + 1, pause [1] + 1]): [New] = poz = [position] [1] Pause [0] + 1, pause [1] -1] result + = downbyte (pyramid), pause) + search_max (pyramid, [pause [0] + 1, pause [1] -1] elif dowrite (pyramid, pause ) + Search_max (pyramid, [pause [0] + 1, pause [1] +1]) & gt; downbyte (pyramid, pause) + search_max (pyramid, [pause [0] + 1, paus [1] -1 ]: New_poz = [pause [0] + 1, pause [1] +1] result = = absolute (pyramid, pos) + find_max (pyramid, [pin [0] + 1, position [1] +1]) Other: Return (results) find_max (pyramid, position)  

A big part of your problem is that you have more than the needs of lots . You should actually make a recurring call only at any time, only by find_max , and you need some base-case logic to stop after the last line.

Try this code:

  def find_max (pyramid, x, y): if y> = Len (pyramid): # base case, we are empty from the bottom of the pyramid return 0, then immediately return to 0, without left_value = address_x (pyramid, x-1, y + 1) # first recurring call right_value = Search_max (pyramid, x + 1, y + 1) # seconds recursive call if left_value & gt; Instead of using the correct use, I changed the call signature to have different values ​​for coordinates. Right_food: return left_value + pyramid [y] [x] second: back right_man + pyramid [y] [x]  

A tube, because it was very easy to index. Call it from find_max (pyramid, 3, 0) , and get rid of the global pos list also gives me the result of results global Have been rid of).

This algorithm can benefit greatly from memoanization, as you will calculate the values ​​on the large pyramid, without memorization at several times in the low-middle areas, the code is impractically slow for large pyramid sizes. Could.


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

How to access user directory in lazarus? -

java - Gradle dependencies: compile project by relative path -