Skip to content

Instantly share code, notes, and snippets.

@jakedobkin
Created December 23, 2011 16:05
Show Gist options
  • Select an option

  • Save jakedobkin/1514569 to your computer and use it in GitHub Desktop.

Select an option

Save jakedobkin/1514569 to your computer and use it in GitHub Desktop.
Problem 67
# http://projecteuler.net/problem=67
# i wrote problem 18 in javascript- http://projecteuler.net/index.php?section=problems&id=18, https://gist.github.com/1380694, so i rewrote it here in python
file = open('triangle.txt','r').readlines()
# this opens the file, cleans it of the line breaks, and then imports it into a 2D array
for i in range (0,len(file)):
file[i]=file[i][0:-2]
array=[]
for j in range (0,len(file)):
array.append([])
for k in range (0,len(file[j]),3):
array[j].append(int(file[j][k:k+2]))
#adding algorithm works backward from next to last row to 0 row
for r in range (98,-1,-1):
for s in range (0,len(array[r])):
array[r][s]=array[r][s]+max(array[r+1][s],array[r+1][s+1])
print "greatest chain total is",array[0][0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment