Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tri = []
- def loadTriangle():
- while len(tri) > 0:
- del tri[0]
- try:
- load = open(input('Filename? -={ '), 'r')
- except FileNotFoundError:
- print('Error: File not found...')
- return
- conts = load.read()
- for line in conts.split('\n'):
- temp = []
- for bit in line.split(' '):
- temp.append(int(bit))
- tri.append(temp)
- for line in tri:
- for bit in line:
- print(bit, end = ' ')
- print()
- def maxPath():
- line = len(tri) - 2
- below = tri[line + 1]
- Idxs = []
- bIdxs = []
- currSums = []
- while line >= 0:
- current = tri[line]
- for i in range(len(current)):
- choices = below[i:i+2]
- bIdxs.append(i + choices.index(max(choices)))
- currSums.append(current[i] + max(choices))
- line -= 1
- below = currSums
- Idxs.insert(0, bIdxs)
- bIdxs = []
- currSums = []
- line = 0
- bigSum = 0
- maxIdx = 0
- while line < len(tri):
- print(tri[line][maxIdx], end = '')
- bigSum += tri[line][maxIdx]
- if line + 1 < len(tri):
- maxIdx = Idxs[line][maxIdx]
- print(' + ', end = '')
- line += 1
- print(' =', bigSum)
- return below[0]
- loadTriangle()
- maxPath()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement