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