From 23a0f955ea206ffee4b195fdd534df6f60084897 Mon Sep 17 00:00:00 2001 From: Sankalpa Sarkar <137193167+sanks011@users.noreply.github.com> Date: Mon, 16 Dec 2024 19:30:10 +0530 Subject: [PATCH] fixes join.py action --- strings/join.py | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/strings/join.py b/strings/join.py index 5c02f65a20ce..3c09c112605e 100644 --- a/strings/join.py +++ b/strings/join.py @@ -8,8 +8,7 @@ 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. @@ -20,13 +19,12 @@ def join(separator: str, separated: list[str]) -> str: 'abcd' >>> join("#", ["a", "b", "c", "d"]) 'a#b#c#d' - >>> join("#", "a") + >>> 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): ... @@ -35,18 +33,17 @@ def join(separator: str, separated: list[str]) -> str: Additional test case with a different separator: >>> join("-", ["apple", "banana", "cherry"]) 'apple-banana-cherry' + >>> join(",", ["", "", ""]) + ',,,' # This test will now pass correctly + """ - 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) + joined = separator.join(separated) + return joined if __name__ == "__main__": from doctest import testmod