Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
iter solution in Clear category for Min and Max by Sim0000
def min(*args, key=lambda x: x):
it = iter(args[0] if len(args) == 1 else args)
minvalue = next(it) # first value
cmin = key(minvalue) # for compare
for v in it: # from second to last
cv = key(v) # for compare
if cv < cmin:
cmin = cv # for compare
minvalue = v
return minvalue
def max(*args, key=lambda x: x):
it = iter(args[0] if len(args) == 1 else args)
maxvalue = next(it) # first value
cmax = key(maxvalue) # for compare
for v in it: # from second to last
cv = key(v) # for compare
if cmax < cv: # use only <
cmax = cv # for compare
maxvalue = v
return maxvalue
# reviced version
# - key function is called only once per each element.
# - use iter() to avoid first flag.
# - use only <
May 8, 2014
Comments: