Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First, naive solution with val/mass solution in Clear category for Treasures by Phucanh_Trannguyen
def treasures(info, limit):
l = len(info)
if l == 0:
return 0
bag = {"golden coin": 0, "silver coin" : 0, "ruby": 0}
lim = int(1000 * limit)
if l == 1 or sum([info[k]['weight'] * info[k]['amount'] for k in info.keys()] ) <= lim:
return [i + ": " + str(info[i]['amount']) for i in bag.keys()]
VOW = [ info[k]['price'] / info[k]['weight'] for k in info.keys()]
currentWeight = 0
while currentWeight < lim :
i = VOW.index(max(VOW))
chosen = list(info.keys())[ i]
amount = min(info[chosen]['amount'], int( (lim - currentWeight) / info[chosen]['weight']) )
currentWeight += info[chosen]['weight'] * amount
bag[chosen] = amount
VOW.pop(i)
del info[chosen]
return [b + ": " + str(bag[b]) for b in bag.keys() if bag[b] > 0 ]
Feb. 26, 2019