/helloworld/helloworld1.py
Python | 39 lines | 37 code | 1 blank | 1 comment | 0 complexity | bcb6d7fa39cc423279ac83698ca5ec1d MD5 | raw file
1SPACE = ' '
2
3'''
4>>> splitstring(' xxx yyy')
5[('xxx', 0), ('yyy', 4)]
6>>> splitstring('x y ')
7[('x', 0), ('y', 2)]
8'''
9
10
11def splitstring(s):
12 '''
13 >>> splitstring(' xxx yyy')
14 ['xxx', 'yyy']
15 >>> splitstring('x y ')
16 ['x', 'y']
17 '''
18
19 print '*** splitstring: `' + s + '`'
20
21 if s == '':
22 return []
23
24 if s[0] == SPACE:
25 return [splitstring(s[1:])
26
27 else:
28 n = -1
29 for e in s:
30 n = n+1
31 if e == SPACE:
32 return [s[:n]] + [splitstring(s[n+1:])]
33
34
35
36
37if __name__ == '__main__':
38 import doctest
39 doctest.testmod()