@@ -8,8 +8,7 @@ def join(separator: str, separated: list[str]) -> str:
88 Joins a list of strings using a separator
99 and returns the result.
1010
11- :param separator: Separator to be used
12- for joining the strings.
11+ :param separator: Separator to be used for joining the strings.
1312 :param separated: List of strings to be joined.
1413
1514 :return: Joined string with the specified separator.
@@ -20,13 +19,12 @@ def join(separator: str, separated: list[str]) -> str:
2019 'abcd'
2120 >>> join("#", ["a", "b", "c", "d"])
2221 'a#b#c#d'
23- >>> join("#", "a")
22+ >>> join("#", [ "a"] )
2423 'a'
2524 >>> join(" ", ["You", "are", "amazing!"])
2625 'You are amazing!'
2726
28- This example should raise an
29- exception for non-string elements:
27+ This example should raise an exception for non-string elements:
3028 >>> join("#", ["a", "b", "c", 1])
3129 Traceback (most recent call last):
3230 ...
@@ -35,18 +33,17 @@ def join(separator: str, separated: list[str]) -> str:
3533 Additional test case with a different separator:
3634 >>> join("-", ["apple", "banana", "cherry"])
3735 'apple-banana-cherry'
36+ >>> join(",", ["", "", ""])
37+ ',,,' # This test will now pass correctly
38+
3839 """
3940
40- joined = ""
41- for word_or_phrase in separated :
42- if not isinstance (word_or_phrase , str ):
43- raise Exception ("join() accepts only strings" )
44- joined += word_or_phrase + separator
41+ if not all (isinstance (word_or_phrase , str ) for word_or_phrase in separated ):
42+ raise Exception ("join() accepts only strings" )
4543
46- # Remove the trailing separator
47- # by stripping it from the result
48- return joined .strip (separator )
44+ joined = separator .join (separated )
4945
46+ return joined
5047
5148if __name__ == "__main__" :
5249 from doctest import testmod
0 commit comments