Discuss / Python / 作业试试

作业试试

Topic source
def trim(s):
    if not isinstance(s, str):
        raise TypeError
    n = len(s)
    l = 0
    while (l < n) and (s[l] == ' '):
        l += 1
    r = n
    while (r > l) and (s[r - 1] == ' '):
        r -= 1    
    return s[l:r] if l < r else ''

缩进怪怪得

def trim(s):
    if not isinstance(s, str):
        raise TypeError
    n = len(s)
    l = 0
    while (l < n) and (s[l] == ' '):
        l += 1
    r = n
    while (r > l) and (s[r - 1] == ' '):
        r -= 1
    return s[l:r] if l < r else ''

  • 1

Reply