From 3b1b70bf0e60dddb14917dd7cad34a41d429df01 Mon Sep 17 00:00:00 2001 From: usman Date: Tue, 3 Dec 2024 03:07:05 +0500 Subject: [PATCH 1/2] Fix the separator issue --- strings/split.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/strings/split.py b/strings/split.py index b62b86d2401f..20ce671c8162 100644 --- a/strings/split.py +++ b/strings/split.py @@ -3,17 +3,20 @@ def split(string: str, separator: str = " ") -> list: Will split the string up into all the values separated by the separator (defaults to spaces) - >>> split("apple#banana#cherry#orange",separator='#') + # >>> split("apple#banana#cherry#orange",separator='#') ['apple', 'banana', 'cherry', 'orange'] - >>> split("Hello there") + # >>> 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=';') + ['', 'abbb', '', 'c', ''] """ split_words = [] @@ -25,6 +28,9 @@ def split(string: str, separator: str = " ") -> list: last_index = index + 1 elif index + 1 == len(string): split_words.append(string[last_index : index + 1]) + + # Append the last segment, including cases where the string ends with the separator + split_words.append(string[last_index:]) return split_words From 07cbe68d0632f32aa652f5cddf0f1bc38c449c6f Mon Sep 17 00:00:00 2001 From: usman Date: Tue, 3 Dec 2024 18:42:56 +0500 Subject: [PATCH 2/2] Fix Join.py File issue --- strings/join.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/strings/join.py b/strings/join.py index 5c02f65a20ce..58147e179fb4 100644 --- a/strings/join.py +++ b/strings/join.py @@ -18,15 +18,19 @@ def join(separator: str, separated: list[str]) -> str: >>> join("", ["a", "b", "c", "d"]) 'abcd' + >>> join("#", ["a", "b", "c", "d"]) 'a#b#c#d' - >>> join("#", "a") + + Single-element list: + >>> join("#", ["a"]) 'a' + + Joining with space as a separator: >>> join(" ", ["You", "are", "amazing!"]) 'You are amazing!' - This example should raise an - exception for non-string elements: + This example should raise an exception for non-string elements: >>> join("#", ["a", "b", "c", 1]) Traceback (most recent call last): ... @@ -35,17 +39,18 @@ def join(separator: str, separated: list[str]) -> str: Additional test case with a different separator: >>> join("-", ["apple", "banana", "cherry"]) 'apple-banana-cherry' + + Handling a list with empty strings: + >>> join(",", ["", "", ""]) + ',,' """ + # Ensure all elements in the list are strings + if not all(isinstance(item, str) for item in separated): + raise Exception("join() accepts only strings") - joined = "" - for word_or_phrase in separated: - if not isinstance(word_or_phrase, str): - raise Exception("join() accepts only strings") - joined += word_or_phrase + separator + # Use Python's built-in join method for simplicity and correctness + return separator.join(separated) - # Remove the trailing separator - # by stripping it from the result - return joined.strip(separator) if __name__ == "__main__":