Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Treasures by brownie57
def treasures(info, limit):
bag = {'golden coin': 0, 'silver coin': 0, 'ruby': 0}
limit *= 1000
for treasure in sorted([item for item in info], \
key=lambda x: info[x]['price']//info[x]['weight'], reverse=True):
n = min((limit, limit//info[treasure]['weight'], info[treasure]['amount']))
bag[treasure] = int(n)
limit -= n * info[treasure]['weight']
return [f'{k}: {v}' for k, v in bag.items() if v != 0]
if __name__ == '__main__':
print("Example:")
print(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))
#These "asserts" using only for self-checking and not necessary for auto-testing
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']
print("Coding complete? Click 'Check' to earn cool rewards!")
Sept. 19, 2018
Comments: