如果删掉前两行,就会报错递归深度不够,添加上之后运行是正确的。请问用如下的代码解决“删除字符前后有未知个数的空格”这一问题可以么?
import sys sys.setrecursionlimit(9000000) #设置递归深度 def trim(s): while s!='' and s[0]=='': s=s[1:] while s!='' and s[-1]=='': s=s[:-1] return trim(s) 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('测试成功!')
Sign in to make a reply
Kairos_hh
如果删掉前两行,就会报错递归深度不够,添加上之后运行是正确的。请问用如下的代码解决“删除字符前后有未知个数的空格”这一问题可以么?