triangles.py

View source code here on GitHub!

Includes

python.src.lib.triangles.reduce_triangle(triangle: Sequence[Sequence[int]]) int
 1from itertools import tee
 2from typing import Sequence
 3
 4
 5def reduce_triangle(triangle: Sequence[Sequence[int]]) -> int:
 6    centering = (len(triangle[-1]) + 1)
 7    potential_totals = [0] * centering
 8    for parent in reversed(triangle):
 9        head1, head2 = tee(iter(potential_totals))
10        next(head2, None)
11        potential_totals = [max((x, y)) + z for x, y, z in zip(head1, head2, parent)]
12    return potential_totals[0]

Tags: combinatorics, path-finding