Skip to content

Commit 8243480

Browse files
committed
fixes split.py
1 parent 23a0f95 commit 8243480

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

strings/split.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,21 @@ def split(string: str, separator: str = " ") -> list:
1717
"""
1818

1919
split_words = []
20-
2120
last_index = 0
21+
2222
for index, char in enumerate(string):
2323
if char == separator:
24-
split_words.append(string[last_index:index])
24+
split_words.append(string[last_index:index]) # Add substring between separators
2525
last_index = index + 1
26-
elif index + 1 == len(string):
27-
split_words.append(string[last_index : index + 1])
28-
return split_words
26+
elif index + 1 == len(string): # If at the last character, handle trailing separator
27+
split_words.append(string[last_index:]) # Add the last part of the string
2928

29+
# If the string ends with a separator, ensure an empty string is added
30+
if string and string[-1] == separator:
31+
split_words.append('')
32+
33+
return split_words
3034

3135
if __name__ == "__main__":
3236
from doctest import testmod
33-
3437
testmod()

0 commit comments

Comments
 (0)