Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
finally solution in Clear category for Min and Max by checinski.szymon
def min(*args, **kwargs):
key = kwargs.get("key", None)
print(list(args))
if key is not None:
if len(args) > 1:
min = args[0]
for value in args:
if kwargs["key"](value) < kwargs["key"](min):
min = value
else:
list1 = args[0]
min = list1[0]
for value in list1:
if kwargs["key"](value) < kwargs["key"](min):
min = value
else:
if len(args) > 1:
min = args[0]
for value in args:
if value < min:
min = value
else:
list1 = args[0]
i = 0
for value in list1:
if i == 0:
min = value
i = 1
elif value < min:
min = value
return min
def max(*args, **kwargs):
key = kwargs.get("key", None)
#print(kwargs["key"])
if key is not None:
if len(args) > 1:
max = args[0]
for value in args:
if kwargs["key"](value) > kwargs["key"](max):
max = value
else:
list1 = args[0]
max = list1[0]
for value in list1:
if kwargs["key"](value) > kwargs["key"](max):
max = value
else:
if len(args) > 1:
max = args[0]
for value in args:
if value > max:
max = value
else:
list1 = args[0]
i = 0
for value in list1:
if i == 0:
max = value
i = 1
elif value > max:
max = value
return max
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
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"
assert min(abs(i) for i in range(-10, 10)) == 0, "dodatkowy"
Oct. 22, 2016