From e50c4d526ee09885c2ffa5ec06c6801029d1da26 Mon Sep 17 00:00:00 2001 From: Aarthi Date: Tue, 3 Dec 2024 21:03:09 +0530 Subject: [PATCH 1/2] Update join.py --- strings/join.py | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/strings/join.py b/strings/join.py index 5c02f65a20ce..998e52ccd8df 100644 --- a/strings/join.py +++ b/strings/join.py @@ -1,32 +1,27 @@ """ Program to join a list of strings with a separator """ - - def join(separator: str, separated: list[str]) -> str: """ Joins a list of strings using a separator and returns the result. - :param separator: Separator to be used - for joining the strings. + :param separator: Separator to be used for joining the strings. :param separated: List of strings to be joined. :return: Joined string with the specified separator. Examples: - + >>> join(",", ["", "", ""]) + ',,' >>> join("", ["a", "b", "c", "d"]) 'abcd' >>> join("#", ["a", "b", "c", "d"]) 'a#b#c#d' - >>> join("#", "a") - 'a' >>> 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): ... @@ -37,15 +32,10 @@ def join(separator: str, separated: list[str]) -> str: 'apple-banana-cherry' """ - 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 + if not all(isinstance(word_or_phrase, str) for word_or_phrase in separated): + raise Exception("join() accepts only strings") - # Remove the trailing separator - # by stripping it from the result - return joined.strip(separator) + return separator.join(separated) if __name__ == "__main__": From f7744a0265b020c65f6a43f1e88a6172259809c6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Dec 2024 15:46:44 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/join.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/strings/join.py b/strings/join.py index 998e52ccd8df..737cef38eae2 100644 --- a/strings/join.py +++ b/strings/join.py @@ -1,6 +1,8 @@ """ Program to join a list of strings with a separator """ + + def join(separator: str, separated: list[str]) -> str: """ Joins a list of strings using a separator