垃圾写的垃圾代码
Topic source学习了,谢谢。 做了一点改动,去除space:
def trim(a): if not isinstance(a,str): raise TypeError('参数必须为str类型') if a==''or a.isspace(): return '' b=list(a) while b[-1].isspace(): b.pop() if b == []: return '' while b[0].isspace(): b.pop(0) return ''.join(b)
测试:
if trim('hello ') != 'hello': print('测试失败! 1') elif trim(' hello') != 'hello': print('测试失败! 2') elif trim(' hello ') != 'hello': print('测试失败! 3') elif trim(' hello world ') != 'hello world': print('测试失败! 4') elif trim('\t\n'+'hello\tword') != 'hello\tword': print('测试失败! 5') elif trim('') != '': print('测试失败! 6') elif trim(' ') != '': print('测试失败! 7') else: print('测试成功!')
- 1
MRZ轲信御马上