Jan-10-2019, 07:39 PM

In my task I have to find the maximum path sum in a triangle of numbers - .txt file (maximum total from top to bottom).

For example in a triangle:

4

34

126 maxpathsum is 10

This is my program:

For example in a triangle:

4

34

126 maxpathsum is 10

This is my program:

def maxSum(triangle, l, n): for i in range(l-1, -1, -1): for j in range(i+1): if (triangle[i+1][j] > triangle[i+1][j+1]): triangle[i][j] += triangle[i+1][j] else: triangle[i][j] += triangle[i+1][j+1] return triangle[0][0] with open("triangle.txt") as fp: total = 0 for count, line in enumerate(fp): triangle = [int(num) for num in line.split()] tri1 = open('triangle.txt', 'r') tri2 = tri1.read() x= len(tri2.splitlines()) y=x-1 #print(triangle) print(maxSum(triangle, y, y))I don't know how to fix this error:

```
Error:Traceback (most recent call last):
File "C:\Users\Mateo\Desktop\course-0\Task1.py", line 27, in <module>
print(maxSum(triangle, y, y))
File "C:\Users\Mateo\Desktop\course-0\Task1.py", line 6, in maxSum
if (triangle[i+1][j] > triangle[i+1][j+1]):
IndexError: list index out of range
```