Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Golden Pyramid by quis20
def count_gold(pyramid):
pyr_dct={(row,column): pyramid[row][column] for row in range(len(pyramid))
for column in range(len(pyramid[row]))}
key_table=[(row,column) for row in range(len(pyramid))
for column in range(row+1)]
pyr_path={}
for i in key_table:
row, column=i
if row == 0:
pyr_path[i] = pyr_dct[i]
continue
if column == 0:
pyr_path[i] = pyr_dct[i] + pyr_path[(row-1,column)]
continue
if column == row:
pyr_path[i] = pyr_dct[i] + pyr_path[(row-1,column-1)]
continue
pyr_path[i] = pyr_dct[i] + max(pyr_path[row-1,
column],pyr_path[row-1,column-1])
pyramid_sums = ''
for i in sorted(pyr_path.keys()):
pyramid_sums += '{}{}'.format(pyr_path[i], ('\n' if i[0] == i[1] else ','))
print(pyramid_sums)
return max(pyr_path.values())
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"
Jan. 26, 2016