Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Recursive generator solution in Clear category for Golden Pyramid by flpo
from itertools import chain
def count_gold(pyramid):
"""
Return max possible sum in a path from top to bottom
"""
def gen_paths(row, col):
if row == len(pyramid) - 1:
yield pyramid[row][col]
else:
for p in chain(gen_paths(row+1, col), gen_paths(row+1, col+1)):
yield pyramid[row][col] + p
return max(gen_paths(0, 0))
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert count_gold((
(1,),
(2, 3),
(3, 3, 1),
(3, 1, 5, 4),
(3, 1, 3, 1, 3),
(2, 2, 2, 2, 2, 2),
(5, 6, 4, 5, 6, 4, 3)
)) == 23, "First example"
assert count_gold((
(1,),
(2, 1),
(1, 2, 1),
(1, 2, 1, 1),
(1, 2, 1, 1, 1),
(1, 2, 1, 1, 1, 1),
(1, 2, 1, 1, 1, 1, 9)
)) == 15, "Second example"
assert count_gold((
(9,),
(2, 2),
(3, 3, 3),
(4, 4, 4, 4)
)) == 18, "Third example"
April 13, 2017
Comments: