A road number triangle maximized using a greedy algorithm. It is not the most efficient way, since I am a beginner, this is the best I can do.

I need your comments on how to improve it in terms of maximizing the total. It works in a way that chooses the highest value of the adjacent numbers in the next row. Without brute force (could take an eternity with large numbers). I also included many functions that do different things (check the documentation strings), how to make a size triangle, read a triangle from a file … I want your comments on how to improve things in terms of optimization / style ….

```
from the time of import
import randomly
def make_row (n, not_str = False):
"" "Makes a row of size n; not_str = True to return integers.
Returns a list containing the row, if not_str = True, a list of strings (numbers) is returned. "" "
temp = []
for i in the range (n):
yes no_str:
temp.append ((random.randint (10, 99)))
plus:
temp.append (str (random.randint (10, 99)))
return temperature
def make_triangle (n, not_str = False):
"" "Make a triangle of numbers of size n.
Returns triangle, a list of lists (rows); if not_str = True, a list of strings is returned otherwise: integer. "" "
triangle = []
for i in rank (1, n + 1):
yes no_str:
temp = make_row (i, not_str)
triangle.append (temp)
plus:
temp = make_row (i)
triangle.append (temp)
return triangle
def save_triangle (n, file name):
"" "Assumes that the file name is a string (name of the file) and n the size of the triangle to be made.
Type triangle in & # 39; file name & # 39; "" "
triangle = make_triangle (n)
triangle_file = open (file name, & # 39; a + & # 39;)
for the row in triangle:
row = & # 39; & # 39 ;. join (row)
triangle_file.writelines (row + & # 39; n & # 39;)
triangle_file.close ()
def triangle_to_list (triangle, p = False):
"" "Assumes that the triangle is a list or a string and p = True to print the triangle.
Returns a list of lists (rows) that contain integers. "" "
# If the triangle is not a list (for example, a .txt file or string, clean new lines and spaces).
If it is not an instance (triangle, list):
rows = triangle.split (& # 39; n & # 39;)
# If p, print the triangle of numbers and size.
Yep:
print (f & # 39; Triangle size {len (rows)} & # 39;)
Print()
for rw in rows:
print (& # 39; & # 39; join (rw))
# Clean spaces
while & # 39; & # 39; in rows:
rows.remove (& # 39; & # 39;)
row_nums = [x.split(' ') for x in rows]
all_rows = []
# Convert strings to integers.
for the row in row_nums:
temp = []
for num in row:
temp.append (int (num))
all_rows.append (temp)
# Returns a list of integers.
back all_rows
# If the triangle is produced using make_triangle, it is a list.
If it is an instance (triangle, list):
rows = triangle
# If p, print the triangle of numbers and size.
Yep:
print (f & # 39; Triangle size {len (rows)} & # 39;)
Print()
# Convert from integers to strings to print using .join ().
list_of_strings_to_print = []
for row in rows:
temp = []
for the number in the row:
temp.append (str (number))
list_of_strings_to_print.append (temp)
for the row in list_of_strings_to_print:
print (& # 39; & # 39; .join (row))
Print()
# Returns a list of integers.
return rows
def triangle_file_list (file name, p = False):
"" "Assumes that & # 39; file name & # 39; contains a triangle of numbers (strings), p = True to print. Returns a list of lists (rows) containing integers." ""
with open (file name, & # 39; r & # 39;) as tri:
triangle_file = tri.read ()
#
Yep:
print (triangle_file)
raw_triangle = & # 39; & # 39; .join (triangle_file)
return triangle_to_list (raw_triangle)
def maxim_path (rows, p = False):
"" "Assumes that the rows are a list of lists that contain all the rows and p = True to print the route.
Returns the total of the maximum route.
""
start = 0
total = 0
# This list contains the number, the index in the row (to avoid calculation errors in the case of row duplicates), the next number).
Choice combinations = []
while I start < len(rows) - 1:
for index, number in enumerate(rows[start]):
next_max = (max(rows[start + 1][index], rows[start + 1][index + 1]))
if next_max == rows[start + 1][index]:
choice_combinations.append((number, index, next_max))
if next_max == rows[start + 1][index + 1]:
choice_combinations.append((number, index + 1, next_max))
start += 1
final_choices = [choice_combinations[0]]
for number, index, next_number in choice_combinations[1:]:
# Performing 2 checks: check by number and check by index.
if number == final_choices[-1][-1] and any((index == final_choices[-1][1], final_choices[-1][1] == index - 1)):
final_choices.append((number, index, next_number))
for item in final_choices:
total += item[0]
total += final_choices[-1][-1]
# If p, print the maximum path, sum.
if p:
print('Maximum path:')
for item in final_choices:
print(f'{item[0]} --> & # 39 ;, end = & # 39; & # 39;)
print (final_choices[-1][-1])
Print()
print (f & # 39; Maximum sum: {total} & # 39;)
total return
def test_triangles (n, one = False):
"" "Assumes that n is the maximum size of the triangles to print, prints n triangles, n routes, n sums, n times.
If one = True, print only one triangle of size n. "" "
time_all = time ()
Yes one:
time1 = time ()
tr = make_triangle (n, True)
rws = triangle_to_list (tr, True)
Maximize the route (rws, True)
Print()
print (f & # 39; Total time: {time () - time_all} seconds. & # 39;)
plus:
for i in range (2, n + 1):
time1 = time ()
tr = make_triangle (i, True)
rws = triangle_to_list (tr, True)
Maximize the route (rws, True)
Print()
print (f & # 39; Time taken: {time () - time1} seconds. & # 39;)
Print()
print (f & # 39; Total time: {time () - time_all} seconds & # 39;)
yes __name__ == & # 39; __ main __ & # 39;
test_triangles (20, True)
```