Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Without OrderedDict solution in Creative category for Treasures by Merzix
def treasures(info, limit):
limit = int(limit * 1000) # kg -> g
items = [{'name':k,\
'cost_per_gramm': v['price'] / v['weight'],\
'weight': v['weight'],
'amount': v['amount'],
'taken': 0,}\
for k,v in info.items()]
for i in sorted(items, key=lambda x: -x['cost_per_gramm']):
i['taken'] = min([limit // i['weight'], i['amount']])
limit -= i['taken'] * i['weight']
if not limit:
break
return sorted([f'{i["name"]}: {i["taken"]}' for i in items if i["taken"] > 0],
key=lambda x: x[3])
if __name__ == '__main__':
assert treasures({'golden coin':
{'price': 100, 'weight': 50, 'amount': 200},
'silver coin':
{'price': 10, 'weight': 20, 'amount': 1000},
'ruby':
{'price': 1000, 'weight': 200, 'amount': 2}}, 5) == [
'golden coin: 92', 'ruby: 2']
assert treasures({'golden coin':
{'price': 100, 'weight': 50, 'amount': 100},
'silver coin':
{'price': 10, 'weight': 20, 'amount': 100},
'ruby':
{'price': 1000, 'weight': 200, 'amount': 1}}, 7.5) == [
'golden coin: 100', 'silver coin: 100', 'ruby: 1']
Sept. 17, 2018