-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path67_max_path_2.py
More file actions
33 lines (28 loc) · 801 Bytes
/
Copy path67_max_path_2.py
File metadata and controls
33 lines (28 loc) · 801 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import time
start = time.time()
triangle = open('67triangle.txt', 'r')
def parse():
tree = []
line = triangle.readline()
while line != '':
tree.append([int(x) for x in line.split()])
line = triangle.readline()
return tree
def check_children(lvl):
sequence = []
for i in range(len(t[lvl])):
l_path = t[lvl + 1][i]
r_path = t[lvl + 1][i + 1]
c_node = t[lvl][i]
if c_node + r_path >= c_node + l_path:
t[lvl][i] = c_node + r_path
else:
t[lvl][i] = c_node + l_path
def do_stuff():
for i in range(len(t) - 1)[::-1]:
check_children(i)
t = parse()
do_stuff()
elapsed = time.time() - start
print("Found an answer in: {} seconds.".format(elapsed))
print("The answer is: {}.".format(t[0][0]))