Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Treasures by mortonfox
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]
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: