The complete problem definition can be summarized as "find the largest sum of for paths from the top to the bottom of a triangle of integers". I decided to read the triangle in from a file which can be found here.
My solution
#!/usr/bin/python
"""chicks' answer to Euler Project problem #18"""
import sys
import string
import re
import pprint
# read command line
argcount = len(sys.argv)
if argcount <= 1:
raise ValueError("missing filename on command line")
triangle_filename = sys.argv[1]
print str(argcount) + " arguments: " + triangle_filename
# read file
with open(triangle_filename, "r") as triangle_file:
lines = triangle_file.readlines()
print str(len(lines)) + " lines read"
triangle = []
for y in lines:
y = string.strip(y)
fields = re.split(r'\s+', y)
int_fields = []
for f in fields:
int_fields.append(int(f))
triangle.insert(0, int_fields)
c = len(fields)
pretty = pprint.PrettyPrinter(indent=4)
#pretty.pprint(triangle)
rows = len(triangle) - 1
for z in range(0, rows):
row = triangle[z]
pretty.pprint(row)
entries = len(row) - 1
for y in range(0, entries):
a = row[y]
b = row[y + 1]
#print "comparing " + str(a) + " and " + str(b)
adder = 0
if a >= b:
adder = a
else:
adder = b
triangle[z + 1][y] += adder
answer = str(triangle[rows][0])
print "ANSWER:" + answer
Desired Focus
I left performance out because I'm more interested in having pythonicly correct and maintainable code than squeezing out a few ms. I have run this through pylint
and fixed everything it suggested. I am still a beginner at python so I'm presuming there are more pythonic ways to do some of this.