函数练习

import random 
def max_min_nums(x,y,*args):
    print(max(x,y,*args),min(x,y,*args))

max_min_nums(4,5,6),max_min_nums(*[random.randint(1,100) for i in range(10)])

 无需解构,大大提升效率

def max_min_nums(x,y,*args):
    mx,mn = max(args),min(args)
    return min(x,y,mn),max(x,y,mx)
max_min_nums(
2,3,1,4,1)
def max_min_nums(x,y,*args):
    mins,*_,maxs = sorted((x,y,*args))
    return mins,maxs
def max_min_nums(x,y,*args):
    mx,mn = (y,x) if y > x else (x,y)
    for x in args:
        if x > mx:
            mx = x 
        elif x < mn:
            mn = x 
    return mx,mn 

 

def get_max_min():
    max_ = min_ = None
    while True:
        nums = input('please input nums').replace(',',' ').split()
        nums = [int(x) for x in nums]
        if not nums:
            break
        if max_ is None:
            max_ = min_ = nums[0]
        max_ = max(max_,*nums)
        min_ = min(min_,*nums)
        print(max_,min_)

 

posted @ 2020-04-11 23:21  Alrenn  阅读(116)  评论(0)    收藏  举报