/helloworld/helloworld1.py

https://bitbucket.org/mariastepanova/good-one · Python · 39 lines · 26 code · 11 blank · 2 comment · 6 complexity · bcb6d7fa39cc423279ac83698ca5ec1d MD5 · raw file

  1. SPACE = ' '
  2. '''
  3. >>> splitstring(' xxx yyy')
  4. [('xxx', 0), ('yyy', 4)]
  5. >>> splitstring('x y ')
  6. [('x', 0), ('y', 2)]
  7. '''
  8. def splitstring(s):
  9. '''
  10. >>> splitstring(' xxx yyy')
  11. ['xxx', 'yyy']
  12. >>> splitstring('x y ')
  13. ['x', 'y']
  14. '''
  15. print '*** splitstring: `' + s + '`'
  16. if s == '':
  17. return []
  18. if s[0] == SPACE:
  19. return [splitstring(s[1:])
  20. else:
  21. n = -1
  22. for e in s:
  23. n = n+1
  24. if e == SPACE:
  25. return [s[:n]] + [splitstring(s[n+1:])]
  26. if __name__ == '__main__':
  27. import doctest
  28. doctest.testmod()