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 10 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
beautifulsoup4
fake_useragent
fake-useragent
imageio
keras
lxml
Expand All @@ -11,7 +11,7 @@ pillow
requests
rich
scikit-learn
sphinx_pyproject
sphinx-pyproject
statsmodels
sympy
tweepy
Expand Down
24 changes: 11 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,17 +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):
Copy link

@Kaaserne Kaaserne Dec 18, 2024

Choose a reason for hiding this comment

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

I think this if statement, with the removal of all, can be placed inside the for-loop. That way we only iterate over the sequence once, instead of twice.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for your advice. sure lemme check

raise Exception("join() accepts only strings")

joined = separator.join(separated)
Copy link

@Kaaserne Kaaserne Dec 16, 2024

Choose a reason for hiding this comment

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

Question about this join though. Isn't the point of this repository to show possible implementations of python functions? Because if that's the case, I was wondering why the join function now internally uses str.join?

Copy link
Author

Choose a reason for hiding this comment

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

okay I understood! Now I'll try to implement something which manually constructs the resulting string without relying on Python's built-in str.join()


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?

# Remove the trailing separator
# by stripping it from the result
return joined.strip(separator)
return joined


if __name__ == "__main__":
Expand Down
17 changes: 13 additions & 4 deletions strings/split.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@ def split(string: str, separator: str = " ") -> list:
"""

split_words = []

last_index = 0

for index, char in enumerate(string):
if char == separator:
split_words.append(string[last_index:index])
split_words.append(
string[last_index:index]
) # Add substring between separators
last_index = index + 1
elif index + 1 == len(string):
split_words.append(string[last_index : index + 1])
elif index + 1 == len(
string
): # If at the last character, handle trailing separator
split_words.append(string[last_index:]) # Add the last part of the string

# If the string ends with a separator, ensure an empty string is added
if string and string[-1] == separator:
split_words.append("")

return split_words


Expand Down
Loading