Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Golden Pyramid by silverspringn81
# accumulate golds from the base to j-th gold of i-th stage
def cnt_acc(i, j, pmid):
if (i + 1) < len(pmid):
return pmid[i][j] + max(cnt_acc(i+1, j, pmid), cnt_acc(i+1, j+1, pmid))
else:
return pmid[i][j]
count_gold = lambda pyramid: cnt_acc(0, 0, list(pyramid))
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"
May 8, 2016