Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes join.py and split.py action and requirements error #12438

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
52 changes: 15 additions & 37 deletions strings/join.py
Original file line number Diff line number Diff line change
@@ -1,49 +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 separated: List of strings to be joined.

:return: Joined string with the specified separator.
Custom implementation of the join() function.
This function manually concatenates the strings in the list,
using the provided separator, without relying on str.join().

Examples:

>>> 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:
>>> join("#", ["a", "b", "c", 1])
Traceback (most recent call last):
...
Exception: join() accepts only strings

Additional test case with a different separator:
>>> join("-", ["apple", "banana", "cherry"])
'apple-banana-cherry'
>>> join(",", ["", "", ""])
',,,' # This test will now pass correctly
:param separator: The separator to place between strings.
:param separated: A list of strings to join.

:return: A single string with elements joined by the separator.

Check failure on line 11 in strings/join.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

strings/join.py:11:1: W293 Blank line contains whitespace
:raises Exception: If any element in the list is not a string.
"""

if not all(isinstance(word_or_phrase, str) for word_or_phrase in separated):
raise Exception("join() accepts only strings")

joined = separator.join(separated)
# Manually handle concatenation
result = ""
for i, element in enumerate(separated):
result += element
if i < len(separated) - 1: # Add separator only between elements
result += separator

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kaaserne can you please review it?

return joined
return result


if __name__ == "__main__":
Expand Down
Loading