Skip to content

Commit ceb2cb9

Browse files
committed
Reject multi-character split separators
1 parent d0f9b6e commit ceb2cb9

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

strings/split.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@ def split(string: str, separator: str = " ") -> list:
1717
1818
>>> split(";abbb;;c;", separator=';')
1919
['', 'abbb', '', 'c', '']
20+
21+
>>> split("a--b--c", separator="--")
22+
Traceback (most recent call last):
23+
...
24+
ValueError: separator must be a single character
2025
"""
2126

27+
if len(separator) != 1:
28+
raise ValueError("separator must be a single character")
29+
2230
split_words = []
2331

2432
last_index = 0

tests/test_split.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
3+
from strings.split import split
4+
5+
6+
def test_split_rejects_multi_character_separator():
7+
with pytest.raises(ValueError, match="separator must be a single character"):
8+
split("a--b--c", separator="--")
9+
10+
11+
def test_split_supports_single_character_separator():
12+
assert split("a--b--c", separator="-") == ["a", "", "b", "", "c"]

0 commit comments

Comments
 (0)