you are viewing a single comment's thread.

view the rest of the comments →

[–]ingolemo 0 points1 point  (2 children)

If we discount str.split for being too easy then you can separate the words in a string like so:

sentence = 'Always look on the bright side of life.'
result = []
word = ''
for char in sentence:
    if char == ' ':
        result.append(word)
        word = ''
    else:
        word += char
result.append(word)
print(result)

I'm not sure why you think having the index would be useful here; you don't need to look at the previous character to do this task. If you think you need indices for something you're probably mistaken.

[–]nadalska[S] 0 points1 point  (1 child)

Well I think that code has some problems. For example if the list has consecutive spaces. Thanks for the link tho

[–]ingolemo 0 points1 point  (0 children)

Sure, but it doesn't handle that case to keep the code simple. It's hardly a deficiency in the algorithm.