You are required to find the greatest path sum starting at t

You are required to find the greatest path sum starting at the top of the triangle and moving only to adjacent numbers on the row below.

Write in python the greedy algorithm in a function (use for reference, http://whatis.techtarget.com/definition/greedy-algorithm)

def greedy(a): #where a = [[3], [7, 4], [2, 4, 6], [8, 5, 9, 3]]

Solution

a = [[3], [7, 4], [2, 4, 6], [8, 5, 9, 3]]
b=[0,0,0,0]
for l in range(1,len(a)):
   curr = b[l-1]
   if a[l][curr] < a[l][curr+1]:
       b[l] = curr+1
   else:
       b[l] = curr
s = \"\"
sum1 = 0
for i in range(0,len(b)):
   sum1+=a[i][b[i]]
   s+=str(a[i][b[i]])+\" \"
print(\"The greatest path sum is: \"+str(sum1))
print(\"For the path: \"+ s)

You are required to find the greatest path sum starting at the top of the triangle and moving only to adjacent numbers on the row below. Write in python the gre

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site