-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path67.py
36 lines (33 loc) · 970 Bytes
/
67.py
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
34
35
36
'''Find the maximum total from top to bottom in triangle.txt
a 15K text file containing a triangle with one-hundred rows.'''
import os.path
import time
start = time.time()
with open(os.path.join(os.path.abspath(''),'files/p067_triangle.txt'), 'r') as f:
r = f.read().strip().split('\n')
M = [list(map(int, c.split())) for c in r]
N = [list(map(int, c.split())) for c in r][::-1]
print(time.time() - start)
# top -> bottom
start = time.time()
for i,c in enumerate(M):
if i == 0:
continue
for j in range(len(c)):
if j == 0:
M[i][j] += M[i-1][j]
elif j == len(c)-1:
M[i][j] += M[i-1][j-1]
else:
M[i][j] += max(M[i-1][j], M[i-1][j-1])
print(max(M[-1]))
print(time.time() - start)
# bottom -> top
start = time.time()
for i,c in enumerate(N):
if i == 0:
continue
for j in range(len(c)):
N[i][j] += max(N[i-1][j], N[i-1][j+1])
print(N[-1][0])
print(time.time() - start)