Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Golden Pyramid by anuj74
def count_gold(pyramid):
triangle_list = list(list(row) for row in pyramid)
for i in range(len(triangle_list)-1,0,-1):
current_row = triangle_list[i] # last row
replace_row = triangle_list[i-1] # penultimate row
for j in range(len(replace_row)):
replace_row[j]=max(current_row[j]+replace_row[j],current_row[j+1]+replace_row[j])
return replace_row[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"
July 4, 2015
Comments: