Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Min and Max by slickLash
from operator import lt, gt
def get_extremum(operator, *args, **kwargs):
iterator = iter(args[0] if len(args) == 1 else args)
extremum = next(iterator)
key = kwargs.get('key', None)
if not key:
for item in iterator:
if operator(item, extremum):
extremum = item
else:
key_extremum = key(extremum)
for item in iterator:
key_item = key(item)
if operator(key_item, key_extremum):
extremum = item
key_extremum = key_item
return extremum
def min(*args, **kwargs):
return get_extremum(lt, *args, **kwargs)
def max(*args, **kwargs):
return get_extremum(gt, *args, **kwargs)
March 4, 2015
Comments: