Discuss / Python / coding

coding

Topic source

def trim(L): start=0 end=len(L) i=0 for i in range(end): if L[start]==' ': start = start+1 elif L[end-1] == ' ': end = end-1 return L[start:end] if trim('hello ')!='hello': print('测试失败') elif trim(' hello')!='hello': print('测试失败') elif trim(' hello ')!='hello': print('测试失败') elif trim(' hello world ')!='hello world': print('测试失败') elif trim('')!='': print('测试失败') elif trim(' ')!='': print('测试失败') else : print('测试成功')

def trim(str): while str[0:1] == ' ' or str[-2:-1] == ' ' : if len(str) == 1: break if str[0:1] == ' ': str = str[1:len(str)] if str[-1:len(str)] == ' ': str = str[0:-1]

return str

  • 1

Reply