From 9e5c669adacc3d8377f512a1f7b6a62c2d592201 Mon Sep 17 00:00:00 2001 From: kishyassin Date: Fri, 6 Dec 2024 01:26:54 +0100 Subject: [PATCH 1/3] Fix split function to handle trailing delimiters correctly --- strings/split.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/strings/split.py b/strings/split.py index b62b86d2401f..f3cb36145a16 100644 --- a/strings/split.py +++ b/strings/split.py @@ -14,6 +14,9 @@ def split(string: str, separator: str = " ") -> list: >>> split("12:43:39",separator = ":") ['12', '43', '39'] + + >>> split(";abbb;;c;", separator=';') + ['', 'abbb', '', 'c', ''] """ split_words = [] @@ -24,11 +27,14 @@ def split(string: str, separator: str = " ") -> list: split_words.append(string[last_index:index]) last_index = index + 1 elif index + 1 == len(string): - split_words.append(string[last_index : index + 1]) + split_words.append(string[last_index:index + 1]) + + if string and string[-1] == separator: + split_words.append('') + return split_words if __name__ == "__main__": from doctest import testmod - - testmod() + testmod() \ No newline at end of file From 393a3c7bf161b0a7973c6f41c95305ebbf2b2e37 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Dec 2024 00:30:33 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/split.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/strings/split.py b/strings/split.py index f3cb36145a16..55568e2b63ce 100644 --- a/strings/split.py +++ b/strings/split.py @@ -27,14 +27,15 @@ def split(string: str, separator: str = " ") -> list: split_words.append(string[last_index:index]) last_index = index + 1 elif index + 1 == len(string): - split_words.append(string[last_index:index + 1]) + split_words.append(string[last_index : index + 1]) if string and string[-1] == separator: - split_words.append('') + split_words.append("") return split_words if __name__ == "__main__": from doctest import testmod - testmod() \ No newline at end of file + + testmod() From 64cbaad82a2f5471206bbcf54082acd376ced0a6 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sun, 29 Dec 2024 14:52:27 +0300 Subject: [PATCH 3/3] Update split.py --- strings/split.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/strings/split.py b/strings/split.py index 55568e2b63ce..ed194ec69c2f 100644 --- a/strings/split.py +++ b/strings/split.py @@ -26,12 +26,8 @@ def split(string: str, separator: str = " ") -> list: if char == separator: split_words.append(string[last_index:index]) last_index = index + 1 - elif index + 1 == len(string): + if index + 1 == len(string): split_words.append(string[last_index : index + 1]) - - if string and string[-1] == separator: - split_words.append("") - return split_words