习题19:

Let's say that the 'slide down' is a sum of consecutive numbers from the top to the bottom of the pyramid. As you can see, the longest 'slide down' is 3 + 7 + 4 + 9 = 23

Your task is to write a function longestSlideDown (in ruby: longest_slide_down) that takes a pyramid representation as argument and returns its' longest 'slide down'.

找出一条从塔尖往下滑的通道,这条通道所有数字相加是最大的。

   /3/
  \7\ 4 
 2 \4\ 6 
8 5 \9\ 3

def longest_slide_down(pyramid):
# TODO: write some code...
# 从0,0开始
l = []
length = len(pyramid)
def steps(x=0, y=0, step=0):
step += pyramid[x][y]
if x == length - 1:
l.append(step)
else:
steps(x+1, y+1, step)
steps(x+1, y, step)
steps()
return max(l)

你可能感兴趣的:(习题19:)