MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_067/sol2.py:18–36  ·  view source on GitHub ↗

Finds the maximum total in a triangle as described by the problem statement above. >>> solution() 7273

()

Source from the content-addressed store, hash-verified

16
17
18def solution() -> int:
19 """
20 Finds the maximum total in a triangle as described by the problem statement
21 above.
22 >>> solution()
23 7273
24 """
25 script_dir = os.path.dirname(os.path.realpath(__file__))
26 triangle_path = os.path.join(script_dir, "triangle.txt")
27
28 with open(triangle_path) as in_file:
29 triangle = [[int(i) for i in line.split()] for line in in_file]
30
31 while len(triangle) != 1:
32 last_row = triangle.pop()
33 curr_row = triangle[-1]
34 for j in range(len(last_row) - 1):
35 curr_row[j] += max(last_row[j], last_row[j + 1])
36 return triangle[0][0]
37
38
39if __name__ == "__main__":

Callers 1

sol2.pyFile · 0.70

Calls 2

splitMethod · 0.80
popMethod · 0.45

Tested by

no test coverage detected