/helloworld/wordnumber.py

https://bitbucket.org/mariastepanova/good-one · Python · 39 lines · 29 code · 8 blank · 2 comment · 7 complexity · ba4cc1b299623267e676f5dad5bf249e MD5 · raw file

  1. SPACE = ' '
  2. def adjustResults(badResults, shift):
  3. '''
  4. >>> adjustResults([('a', 0), ('bbb', 13)], 3)
  5. [('a', 3), ('bbb', 16)]
  6. '''
  7. goodResults = []
  8. for word, pos in badResults:
  9. goodResults.append((word, pos + shift))
  10. return goodResults
  11. def splitstring(s):
  12. '''
  13. >>> splitstring('xxx yyyy zzzz ')
  14. [('xxx', 0), ('yyyy', 4), ('zzzz', 9)]
  15. >>> splitstring(' x y ')
  16. [('x', 1), ('y', 3)]
  17. '''
  18. if s == '':
  19. return []
  20. if s[0] == SPACE:
  21. return adjustResults(splitstring(s[1:]), 1)
  22. else:
  23. n = -1
  24. for e in s:
  25. n = n + 1
  26. if e == SPACE:
  27. return [(s[:n], 0)] + adjustResults(splitstring(s[n + 1:]), n + 1)
  28. return [(s, 0)]
  29. if __name__ == '__main__':
  30. import doctest
  31. doctest.testmod()