Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Treasures by _Chico_
treasure_types = ('golden coin', 'silver coin', 'ruby')
def treasures(info, limit):
unit_cost = dict((name, v['price'] / v['weight']) for name, v in info.items())
taken = {}
limit *= 1000
for name in sorted(treasure_types, key=lambda x: unit_cost[x], reverse=True):
t_info = info[name]
count = min(t_info['amount'], limit // t_info['weight'])
if count > 0:
taken[name] = count
limit -= count * t_info['weight']
return ['%s: %d' % (name, taken[name]) for name in treasure_types if name in taken]
Aug. 26, 2021