File tree Expand file tree Collapse file tree 1 file changed +9
-6
lines changed
Expand file tree Collapse file tree 1 file changed +9
-6
lines changed Original file line number Diff line number Diff 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
3135if __name__ == "__main__" :
3236 from doctest import testmod
33-
3437 testmod ()
You can’t perform that action at this time.
0 commit comments