Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
"recursive" approach solution in Clear category for Golden Pyramid by tomires
def gothrough(posx, posy, gold): # my recursive function
global MAXGOLD # we are going to change the value
# of MAXGOLD, let's define it as a global variable
gold += PYRAMID[posx][posy] # add gold on each step
if(posx == len(PYRAMID) - 1): # have we reached the end of our pyramid?
if(gold > MAXGOLD): MAXGOLD = gold # ... if we have top score, record it
else: # are there still some floors to go?
gothrough(posx + 1, posy, gold) # ... let's walk to the left
gothrough(posx + 1, posy + 1, gold) # ... and to the right as well
return 0
def count_gold(pyramid):
"""
Return max possible sum in a path from top to bottom
"""
global MAXGOLD # this is where we store the max value
MAXGOLD = 0 # the preferred way would be to do it
# using the return value on the recursive
global PYRAMID # function, however I was far too lazy ;P
PYRAMID = pyramid # copy the tuple to a global variable
gothrough(0,0,0) # make wheels spin
#replace this for solution
return MAXGOLD
May 1, 2014
Comments: