-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler067.py
executable file
·35 lines (24 loc) · 903 Bytes
/
euler067.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/python
import sys
if sys.version_info[0] == 2:
# get rid of 2.x range that produced list instead of iterator
range = xrange
def genTriangleLines(triangleFile):
with open(triangleFile, 'r') as fIn:
for line in fIn:
yield [int(n) for n in line[:-1].split()]
def findTrianglePath(triangleFile, pathTypeStr='max'):
pathType = eval(pathTypeStr)
triangleLines = genTriangleLines(triangleFile)
cost = next(triangleLines)
for triangleLine in triangleLines:
triangleLine[0] += cost[0]
triangleLine[-1] += cost[-1]
for n in range(1, len(cost)):
triangleLine[n] += pathType(cost[n-1], cost[n])
cost = triangleLine
print('%simum path sum is %d' % (pathTypeStr.title(), pathType(cost)))
def euler67(triangleFile='data/euler067.txt', pathTypeStr='max'):
findTrianglePath(triangleFile, pathTypeStr)
if __name__ == "__main__":
euler67()