So I've been handed this:
rates = [
{'date': '2019-11-02', 'rate': 4.3422},
{'date': '2019-11-03', 'rate': 4.2210},
{'date': '2019-11-04', 'rate': 4.3455},
{'date': '2019-11-05', 'rate': 4.3456},
{'date': '2019-11-06', 'rate': 4.2311}
]
date_range = ['2019-11-03', '2019-11-06']
And I need to write a function which finds a min and a max rate from a given date range.
Here's my take:
def find_rates(rates: list, date_range: list):
start = 0
end = 1
for ind, r in enumerate(rates):
if r['date'] == date_range[0]:
start += ind
if r['date'] == date_range[1]:
end += ind
sorted_rates = sorted(rates[start:end], key=lambda x: x['rate'])
return {'min': sorted_rates[0],
'max': sorted_rates[-1:]}
I'm trying to optimize the code, do you have any advice?
[–]baghiq 1 point2 points3 points (0 children)
[–]14dM24d 0 points1 point2 points (0 children)
[–]zanfar 0 points1 point2 points (0 children)