From 29db319efc4b8df673df3e5e8592e6a1d43d5598 Mon Sep 17 00:00:00 2001 From: Jeeva varshan <143276421+Jeeva1070@users.noreply.github.com> Date: Tue, 2 Jun 2026 23:38:56 +0530 Subject: [PATCH 1/2] Enhance split function with separator validation Added validation for separator length and improved docstring formatting. --- strings/split.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/strings/split.py b/strings/split.py index ed194ec69c2f..54501841e4db 100644 --- a/strings/split.py +++ b/strings/split.py @@ -1,33 +1,36 @@ def split(string: str, separator: str = " ") -> list: """ - Will split the string up into all the values separated by the separator - (defaults to spaces) - + Will split the string up into all the values separated by the separator (defaults to spaces) >>> split("apple#banana#cherry#orange",separator='#') ['apple', 'banana', 'cherry', 'orange'] - >>> split("Hello there") ['Hello', 'there'] - >>> split("11/22/63",separator = '/') ['11', '22', '63'] - >>> split("12:43:39",separator = ":") ['12', '43', '39'] - >>> split(";abbb;;c;", separator=';') ['', 'abbb', '', 'c', ''] """ + if len(separator) != 1: + raise ValueError("separator must be exactly one character long") - split_words = [] + # If the input string is empty, Python's split returns [''] + if not string: + return [""] + split_words = [] last_index = 0 + + # Single-pass scan using string slicing for optimal memory and speed for index, char in enumerate(string): if char == separator: split_words.append(string[last_index:index]) last_index = index + 1 - if index + 1 == len(string): - split_words.append(string[last_index : index + 1]) + + # Append the remaining trailing substring after the last separator + split_words.append(string[last_index:]) + return split_words From ee161c1c5df3fd83c62ea1297e45e4b9130c6b94 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 2 Jun 2026 18:09:40 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/split.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/strings/split.py b/strings/split.py index 54501841e4db..18010fe1ddde 100644 --- a/strings/split.py +++ b/strings/split.py @@ -21,16 +21,16 @@ def split(string: str, separator: str = " ") -> list: split_words = [] last_index = 0 - + # Single-pass scan using string slicing for optimal memory and speed for index, char in enumerate(string): if char == separator: split_words.append(string[last_index:index]) last_index = index + 1 - + # Append the remaining trailing substring after the last separator split_words.append(string[last_index:]) - + return split_words