Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions strings/split.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,38 @@ def split(string: str, separator: str = " ") -> list:
Will split the string up into all the values separated by the separator
(defaults to spaces)
Comment on lines 3 to 4

>>> split("apple#banana#cherry#orange",separator='#')
>>> split("apple#banana#cherry#orange", separator="#")
['apple', 'banana', 'cherry', 'orange']

>>> split("Hello there")
['Hello', 'there']

>>> split("11/22/63",separator = '/')
>>> split("11/22/63", separator="/")
['11', '22', '63']

>>> split("12:43:39",separator = ":")
>>> split("12:43:39", separator=":")
['12', '43', '39']

>>> split(";abbb;;c;", separator=';')
>>> split(";abbb;;c;", separator=";")
['', 'abbb', '', 'c', '']

>>> split("apple--banana--cherry", separator="--")
Traceback (most recent call last):
...
ValueError: separator should be a single character

>>> split("apple", separator="")
Traceback (most recent call last):
...
ValueError: separator should not be empty
"""

if not separator:
raise ValueError("separator should not be empty")

if len(separator) != 1:
raise ValueError("separator should be a single character")
Comment on lines +32 to +36

split_words = []

last_index = 0
Expand Down