Python练习题答案: 范围功能【难度:2级】--景越Python编程实例训练营,1000道上机题等你来挑战

范围功能【难度:2级】:

答案1:

def range_function(*args, start=1, step=1):
    if len(args) == 1:
        stop = args[0]
    elif len(args) == 2:
        start, stop = args
    else:
        start, step, stop = args
    return range(start, stop+1, step)

答案2:

range_function=lambda a,b=None,c=None:range(not b or a,(c or b or a)+1,not c or b)

答案3:

def range_function(*args):
    if len(args) == 1: return list(range(1,args[0]+1))
    elif len(args) == 2: return list(range(args[0], args[1]+1))
    else: return list(range(args[0],args[2]+1,args[1]))

答案4:

def range_function(*args):
    start, stop, step = 1,1,1
    if len(args)==1: stop,=args
    if len(args)==2: start,stop=args
    if len(args)==3: start, step, stop=args
    s=[]
    i=start
    while i <= stop:
        s.append(i)
        i+=step
    return s​

答案5:

def range_function(*args, start = 1, step = 1):
    if len(args) == 1:
        stop = args[0]
    if len(args) == 2:
        start, stop = args
    if len(args) == 3:
        start, step, stop = args
    return range(start, stop + 1, step)

答案6:

def range_function(start, step=None, end=None):
    if step is None:
        start, step, end = 1, 1, start
    elif end is None:
        step, end = 1, step
    return list(range(start, end+1, step))

答案7:

def range_function(*a):
    if len(a) == 1:
        return range(1, a[0] + 1)
    elif len(a) == 2:
        return range(a[0], a[1] + 1)
    return range(a[0], a[-1] + 1, a[1])

答案8:

def range_function(n1, n2=None, n3=None):
    if n2 != None and n3 != None: return range(n1, n3+1, n2)
    elif n3 == None and n2 != None: return range(n1, n2+1)
    else: return range(1, n1+1)

答案9:

def range_function(*a):
    return range(a[0] if len(a) > 1 else 1, a[-1] + 1, a[1] if len(a) == 3 else 1)

答案10:

def range_function(a, b=None, c=None):
    return range(a, c+1, b) if None not in (b, c) else range(a, b+1) if b is not None else range(1, a+1)

你可能感兴趣的:(Python编程习题答案)