I am a beginner with python and programming in general. I started teaching myself about 2 months ago.
I've been working my way through Project Euler and a few similar sites to build my chops, and because I find it fun/rewarding. Below is my first attempt at some simple dynamic programming to come up with the solution to problem 67.
I would appreciate any criticism or advice. One thing in particular that I struggle with is readability (naming variables, comments, etc.). Also, I am largely ignorant when it comes to convention/best practices, so guidance here would be helpful.
pyr = []
with open('problem_67.txt') as f:
for line in f:
pyr.append(map(int, line.split()))
f.closed
def best_path(li):
"""Find the max total from top to bottom in a triangular array."""
saver = {}
n = len(li)
# Set values for all points to 0
for y in xrange(2, n+1):
for x in xrange(len(li[n-y])):
saver[(n-y, x)] = 0
# Establish the max values for the bottom row
for x in xrange(len(li[n-2])):
a, b, c = li[n-2][x], li[n-1][x], li[n-1][x+1]
saver[(n-2, x)] += max(a+b, a+c)
# Add the highest dict of the two lower options for each other row
for y in xrange(3, n+1):
for x in xrange(len(li[n-y])):
a, b, c = li[n-y][x], saver[(n-(y-1), x)], saver[(n-(y-1), x+1)]
saver[(n-y, x)] += a + max(b, c)
return saver[0, 0]
def main():
print best_path(pyr)
if __name__ == "__main__":
main()