Created
May 28, 2014 17:39
-
-
Save GuillermoPena/363a006ee6f16edc7b0f to your computer and use it in GitHub Desktop.
CheckIO - Home Challenge 9 : Min and Max
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CheckIO - Home Challenge 9 : Min and Max | |
# http://checkio.org | |
# In this mission you should write you own py3 realisation of the built-in functions min and max. | |
# Some builtin functions are closed here: import, eval, exec, globals. | |
# Don't forget you should realize two functions in your code. | |
# max(iterable, *[, key]) or min(iterable, *[, key]) | |
# max(arg1, arg2, *args[, key]) or min(arg1, arg2, *args[, key]) | |
# Return the largest (smallest) item in an iterable or the largest(smallest) of two or more arguments. | |
# If one positional argument is provided, it should be an iterable. | |
# The largest (smallest) item in the iterable is returned. | |
# If two or more positional arguments are provided, the largest (smallest) of the positional arguments is returned. | |
# The optional keyword-only key argument specifies a function of one argument | |
# that is used to extract a comparison key from each list element (for example, key=str.lower). | |
# If multiple items are maximal (minimal), the function returns the first one encountered. | |
# -- Python Documentation (Built-in Functions - https://docs.python.org/3.3/library/functions.html) | |
# Input: One positional argument as an iterable or two or more positional arguments. Optional keyword argument as a function. | |
# Output: The largest item for the "max" function and the smallest for the "min" function. | |
# Precondition: All test cases are correct and functions don't have to raise exceptions. | |
def minSimple(arg1, arg2, key): | |
if key!=None and key(arg1)<key(arg2) or key==None and arg1<arg2: return arg1 | |
return arg2 | |
def min(*args, **kwargs): | |
key=kwargs.get("key", None) # Extracting key | |
if len(args)==1: args=list(args[0]) # Adapting arguments | |
# Comparing arguments, one by one | |
result=args[0] | |
for x in range(1,len(args)): | |
result=minSimple(result,args[x],key) | |
return result | |
def maxSimple(arg1, arg2, key): | |
if key!=None and key(arg2)>key(arg1) or key==None and arg2>arg1: return arg2 | |
return arg1 | |
def max(*args, **kwargs): | |
key=kwargs.get("key", None) # Extracting key | |
if len(args)==1: args=list(args[0]) # Adapting arguments | |
# Comparing arguments, one by one | |
result=args[0] | |
for x in range(1,len(args)): | |
result=maxSimple(result,args[x],key) | |
return result | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment