Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
21-liner: first proposal with no bechmarks solution in Speedy category for Bigger Price by przemyslaw.daniel
def bigger_price(limit, data):
'''
For large data and small limit it is better to find max values on the list
one by one, the boundry can be calculated by having regard to sorting complexity
therefore limit*n is versus n*log(n, 2) where n is data size
'''
from math import log
size = len(data)
if limit+1 > log(size, 2): # +1 for finding min value
# it is also possible to speed up
# calcuations of log base 2 (not this time)
return sorted(data, key=lambda x: x['price'], reverse=True)[:limit]
min_value = min(data, key=lambda x: x['price']) # O(n)
result = []
for _ in range(limit):
max_index = max(range(size), key=lambda x: data[x]['price']) # O(n)
result += [data[max_index]]
data[max_index] = min_value # we do not want to pop from the list here
# as this has O(n) complexity, let's use O(1)
return result
Jan. 2, 2019
Comments: