-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest12.py
More file actions
37 lines (33 loc) · 934 Bytes
/
test12.py
File metadata and controls
37 lines (33 loc) · 934 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# -*- coding=utf-8 -*-
def trim(s):
length = len(s)
if length != 0:
if s[0] == ' ':
return trim(s[1:])
if s[-1] == ' ':
return trim(s[:-1])
for i in range(length-1):
if s[i] == s[i+1] == ' ':
return trim(s[0:i] + s[i+1:])
#sl = s.split(' ')
#while '' in sl:
# sl.remove('')
#s = ' '.join(sl)
return 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(' hello world add d') != 'hello world add d':
print('测试失败!')
elif trim('') != '':
print('测试失败!')
elif trim(' ') != '':
print('测试失败!')
else:
print('测试成功!')