- 1
Write algorithm to find sum of the temperature of two test tubes which will produce the maximum temperature when combined.
2 Respuestas
0
And your try?
0
# python3
from itertools import combinations
tubes = [{"num": 0, "temperature": 10}, {"num": 2, "temperature": 50}, {"num": 3, "temperature": 40}]
# print(list(combinations(tubes, 2)))
# [({'num': 0, 'temperature': 10}, {'num': 2, 'temperature': 50}), ({'num': 0, 'temperature': 10}, {'num': 3, 'temperature': 40}), ({'num': 2, 'temperature': 50}, {'num': 3, 'temperature': 40})]
# calc the sum of the temperature of each combination
result = list(map(lambda x: (x, x[0]["temperature"] + x[1]["temperature"]), combinations(tubes, 2)))
# sorted by sum of temperature desc
result.sort(key=lambda x: x[1], reverse=True)
print(result[0])
# (({'num': 2, 'temperature': 50}, {'num': 3, 'temperature': 40}), 90)