On the level Mountain Mercenaries in Cloudrip Mountain, I’m attempting to collect coins based on their value per distance.
max() fails to select the highest value per distance coin out of all found coins.
def valueOverDistance(coin): return coin.value / hero.distanceTo(coin) useMax = False useMax = True # comment this line out to use the working solution, which doesn't use max while True: coins = hero.findItems() if len(coins) > 0: if useMax: # these don't work as expected #bestCoin = max(coins, key=valueOverDistance) #bestCoin = max(coins, key=lambda coin: valueOverDistance(coin)) bestCoin = max(coins, key=lambda coin: coin.value / hero.distanceTo(coin)) # how does this not work??? #bestCoin = max(coins) # all the lines above using max() have the same result as this line! else: # this works perfectly as expected, but it doesn't use max() bestCoin = coins for coin in coins: if valueOverDistance(coin) > valueOverDistance(bestCoin): bestCoin = coin hero.move(bestCoin.pos) #worstCoin = min(coins, key=valueOverDistance) #doesn't work as expected either #hero.move(worstCoin.pos) ...
Is there a way to use
max() so that it works as expected?