Project Euler - Problem 18


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/usr/bin/python
  2.  
  3. # Project Euler - Problem 18
  4.  
  5. f = open('problem18.tri', 'r')
  6.  
  7. tri =[]
  8. maxvals = []
  9. # Load data into 15 row pryamid list
  10. for m in xrange(15):
  11. tri.append([])
  12. maxvals.append([])
  13. for n in xrange(m+1):
  14. tri[m].append(int(f.read(3)))
  15. if m == 0:
  16. maxvals[m].append(tri[0][0])
  17. elif n == 0:
  18. maxvals[m].append(tri[m][0]+maxvals[m-1][0])
  19. elif n == m:
  20. maxvals[m].append(tri[m][m]+maxvals[m-1][m-1])
  21. else:
  22. maxvals[m].append(tri[m][n]+max(maxvals[m-1][n-1],maxvals[m-1][n]))
  23.  
  24. print max(maxvals[m])

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.