/ Published in: Python
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#!/usr/bin/python # Project Euler - Problem 18 f = open('problem18.tri', 'r') tri =[] maxvals = [] # Load data into 15 row pryamid list for m in xrange(15): tri.append([]) maxvals.append([]) for n in xrange(m+1): tri[m].append(int(f.read(3))) if m == 0: maxvals[m].append(tri[0][0]) elif n == 0: maxvals[m].append(tri[m][0]+maxvals[m-1][0]) elif n == m: maxvals[m].append(tri[m][m]+maxvals[m-1][m-1]) else: maxvals[m].append(tri[m][n]+max(maxvals[m-1][n-1],maxvals[m-1][n])) print max(maxvals[m])