Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Min and Max solution in Uncategorized category for Min and Max by przemyslaw.daniel
def min_max(*args, key=lambda x: x, switch=0):
it = iter(args if len(args)>1 else args[0])
ret = next(it)
for i in it:
ret=i if switch^int(key(i)<=key(ret))==1 else ret
return ret
def min(*args, key=lambda x: x):
return min_max(*args, key=key, switch=0)
def max(*args, key=lambda x: x):
return min_max(*args, key=key, switch=1)
if __name__ == '__main__':
assert max(3, 2) == 3, "Simple case max"
assert min(3, 2) == 2, "Simple case min"
assert max([1, 2, 0, 3, 4]) == 4, "From a list"
assert min("hello") == "e", "From string"
assert max(2.2, 5.6, 5.9, key=int) == 5.6, "Two maximal items"
assert min([[1, 2], [3, 4], [9, 0]], key=lambda x: x[1]) == [9, 0], "lambda key"
May 3, 2016
Comments: