Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Single generic function solution in Clear category for Min and Max by virzen
def lt(val1, val2):
return val1 < val2
def gt(val1, val2):
return val1 > val2
def find(cmp, *args, **kwargs):
"""
Generic find function, which can be used to create specific
one by passing proper comparison (cmp) function.
Arguments:
cmp -- comparison function
args -- arguments, can be an array or argument on after another
kwargs -- named arguments, used to get key - function applied
to arguments before comparison
"""
if len(args) < 1:
return None
key = kwargs.get("key", lambda x: x)
arguments = list(args[0]) if len(args) == 1 else args
result = arguments[0]
for val in arguments:
if cmp(key(val), key(result)):
result = val
return result
min = lambda *args, **kwargs: find(lt, *args, **kwargs)
max = lambda *args, **kwargs: find(gt, *args, **kwargs)
Dec. 13, 2016