SPACE def splitstring splitstring xxx yyy xxx yyy splitstring splitstr

 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
SPACE = ' '
def splitstring(s):
'''
>>> splitstring(' xxx yyy')
['xxx', 'yyy']
>>> splitstring('x y ')
['x', 'y']
>>> splitstring ('xxx xy yyy yyy')
['xxx', 'xy', 'yyy', 'yyy']
'''
n = -1
for e in s:
n = n + 1
if e == SPACE:
if n == 0:
return splitstring (s[1:])
elif n == -1:
return splitstring (s[:-1])
return [s[:n]] + splitstring(s[n+1:])
return [s]
if __name__ == '__main__':
import doctest
doctest.testmod()