I'm trying to solve Problem 18 from Project Euler. My code is:
lines = []
lines.append([3])
lines.append([7, 4])
lines.append([2, 4, 6])
lines.append([8, 5, 9, 3])
i = len(lines) - 1
while i != -1:
for j in range(0, len(lines[i - 1])):
a = lines[i][j]
b = lines[i][j + 1]
if a > b:
lines[i - 1][j] = a
else:
lines[i - 1][j] = b
i -= 1
the result in the console is:
E:\path_to_python\Python3\python.exe E:/path_to_python/Projects/ProjectEuler/18.py
Traceback (most recent call last):
File "E:/path_to_script/18.py", line 33, in <module>
b = lines[i][j + 1]
IndexError: list index out of range
Process finished with exit code 1
the confusing part is that
print("lines[{}][{}] = {} > {} = lines[{}][{}]".format(i,j,lines[i][j], lines[i][j+1], i,j+1))
creates the output:
Traceback (most recent call last):
File "E:/path_to_scripter/18.py", line 31, in <module>
print("lines[{}][{}] = {} > {} = lines[{}][{}]".format(i,j,lines[i][j], lines[i][j+1], i,j+1))
IndexError: list index out of range
lines[3][0] = 8 > 5 = lines[3][1]
lines[3][1] = 5 > 9 = lines[3][2]
lines[3][2] = 9 > 3 = lines[3][3]
lines[2][0] = 8 > 9 = lines[2][1]
lines[2][1] = 9 > 9 = lines[2][2]
lines[1][0] = 9 > 9 = lines[1][1]
so every element exists but is at the same time out of range? What is my mistake here?
Here is a visual of the code at the point of error:
So you can see that at line 11, you are trying to access the element at index 1 of the 0th sub-array, but this array only has one element at index 0, thus the out-of-range error.
Hope this helps you to debug and solve that challenge :D