/helloworld/wordnumber.py
https://bitbucket.org/mariastepanova/good-one · Python · 39 lines · 29 code · 8 blank · 2 comment · 7 complexity · ba4cc1b299623267e676f5dad5bf249e MD5 · raw file
- SPACE = ' '
-
- def adjustResults(badResults, shift):
- '''
- >>> adjustResults([('a', 0), ('bbb', 13)], 3)
- [('a', 3), ('bbb', 16)]
- '''
- goodResults = []
- for word, pos in badResults:
- goodResults.append((word, pos + shift))
- return goodResults
-
- def splitstring(s):
- '''
- >>> splitstring('xxx yyyy zzzz ')
- [('xxx', 0), ('yyyy', 4), ('zzzz', 9)]
- >>> splitstring(' x y ')
- [('x', 1), ('y', 3)]
- '''
-
-
- if s == '':
- return []
-
- if s[0] == SPACE:
- return adjustResults(splitstring(s[1:]), 1)
- else:
- n = -1
- for e in s:
- n = n + 1
- if e == SPACE:
- return [(s[:n], 0)] + adjustResults(splitstring(s[n + 1:]), n + 1)
- return [(s, 0)]
-
-
-
- if __name__ == '__main__':
- import doctest
- doctest.testmod()