Skip to content

Commit

Permalink
fixes join.py action
Browse files Browse the repository at this point in the history
  • Loading branch information
sanks011 committed Dec 16, 2024
1 parent 98391e3 commit 23a0f95
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions strings/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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):
...
Expand All @@ -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
Expand Down

0 comments on commit 23a0f95

Please sign in to comment.