I have to find the largest sum down a triangle of numbers where 1 is the first row, the second row is 3,2, the third is 4,5,6 and so on for n rows where every other row is reversed. The path only goes from top to bottom and does not go sideways or up. The program must sum across the triangle. My code for this problem works effectively for small numbers, like up to 2000, but takes much too long for anything larger. The program has to work for n = 9,999.
My approach was to put each row into a list of lists, called pyramid_list
, and then as long as the list is not as long as the final row, append zeroes onto the end, and the sum the first row in pyramid_list
with the numbers under it.
E.g. for n = 3,
pyramid_list = [[1,0,0], [3,2,0], [4,5,6]]
So the implementation of the addition would look like:
100
320
456
becomes:
430
456
which finally becomes:
8, 9, 9
and the program would output 9
. Each number in the above can only be added to the one directly under it and the one under it to the right.
Here is my code:
# get user input for number of rows
rows = int(input("Enter number of rows: "))
# define number so starting row is always 1
number = 1
# create rows_list
rows_list = []
# total pyramid
pyramid_list = []
if rows == 1:
print(1)
else:
# making a list for each row, then appending each row to one big list
for i in range(1, rows+1):
if i % 2 == 0:
for j in range(1, i+1):
rows_list.append(number)
number += 1
pyramid_list.append((list(reversed(rows_list))))
rows_list = []
else:
for j in range(1, i+1):
rows_list.append(number)
number += 1
if rows_list != []:
pyramid_list.append(rows_list)
rows_list = []
# filling all empty spaces in a row with zeroes
for i in pyramid_list:
while len(i) < rows:
i.append(0)
final = False
while final == False:
# creating a list for the modified row
current_step = []
# where j is the index of each number in the list
for j in range(rows):
if j == 0:
# add first item from current row and the one under it
current_step.append((pyramid_list[0][j]) + pyramid_list[1][j])
# If the current item in the list is not the last one before all the zeroes, find the max addition of it and the 2 items below it
if pyramid_list[0][j+1] != 0:
current_step.append(max(pyramid_list[0][j] + pyramid_list[1][j+1], pyramid_list[0][j+1] + pyramid_list[1][j + 1]))
# For end of row (diagonal path on the right), add the number on the bottom right
if pyramid_list[0][j+1] == 0:
current_step.append(pyramid_list[0][j] + pyramid_list[1][j+1])
# Adding zeroes to the final result of that row, take out the two rows that have just been added together, and put the current step onto the front of the list
while len(current_step) < rows:
current_step.append(0)
pyramid_list.remove(pyramid_list[0])
pyramid_list.remove(pyramid_list[0])
pyramid_list.insert(0, current_step)
# If the addition is done, stop the loop
if len(pyramid_list) == 1:
final = True
break
# Printing the greatest number from the final step
print(max(pyramid_list[0]))
I know that the problem is that the program keeps recomputing numbers it's already computed, but I'm not sure how to change it.