Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Recursion solution in Clear category for Golden Pyramid by kendriu
def count_gold(pyramid):
def count(pyramid, level, to, number):
here = number + pyramid[level][to]
if level == len(pyramid) - 1:
return here
else:
return max(
count(pyramid, level + 1, to, here) ,
count(pyramid, level + 1, to + 1, here),
)
return count(pyramid, 0, 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"
Sept. 1, 2014
Comments: