Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Treasures by mpohily
def treasures(info, limit):
limit *= 1000
take = []
# find most valuable item
value = []
for item in info:
value.append((info[item]['price'] / info[item]['weight'], item))
for item in sorted(value, reverse=True):
take.append(item[1])
# find what we take
for item in take:
info[item]['take'] = 0
while info[item]['amount'] and info[item]['weight'] <= limit:
info[item]['take'] += 1
limit -= info[item]['weight']
info[item]['amount'] -= 1
# result output
take = ['golden coin', 'silver coin', 'ruby']
result = []
for item in take:
if info[item]['take']:
tmp = info[item]['take']
result += [item + f": {tmp}"]
return result
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!")
March 13, 2019