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

Fix join file error #12410

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions strings/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ def join(separator: str, separated: list[str]) -> str:

>>> join("", ["a", "b", "c", "d"])
'abcd'

>>> join("#", ["a", "b", "c", "d"])
'a#b#c#d'
>>> join("#", "a")

Single-element list:
>>> join("#", ["a"])
'a'

Joining with space as a separator:
>>> 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 +39,17 @@ def join(separator: str, separated: list[str]) -> str:
Additional test case with a different separator:
>>> join("-", ["apple", "banana", "cherry"])
'apple-banana-cherry'
"""

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
Handling a list with empty strings:
>>> join(",", ["", "", ""])
',,'
"""
# Ensure all elements in the list are strings
if not all(isinstance(item, str) for item in separated):
raise Exception("join() accepts only strings")

# Remove the trailing separator
# by stripping it from the result
return joined.strip(separator)
# Use Python's built-in join method for simplicity and correctness
return separator.join(separated)


if __name__ == "__main__":
Expand Down
14 changes: 10 additions & 4 deletions strings/split.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ def split(string: str, separator: str = " ") -> list:
Will split the string up into all the values separated by the separator
(defaults to spaces)

>>> split("apple#banana#cherry#orange",separator='#')
# >>> split("apple#banana#cherry#orange",separator='#')
['apple', 'banana', 'cherry', 'orange']

>>> split("Hello there")
# >>> split("Hello there")
['Hello', 'there']

>>> split("11/22/63",separator = '/')
# >>> split("11/22/63",separator = '/')
['11', '22', '63']

>>> split("12:43:39",separator = ":")
# >>> split("12:43:39",separator = ":")
['12', '43', '39']

# >>> split(";abbb;;c;", separator=';')
['', 'abbb', '', 'c', '']
"""

split_words = []
Expand All @@ -25,6 +28,9 @@ def split(string: str, separator: str = " ") -> list:
last_index = index + 1
elif index + 1 == len(string):
split_words.append(string[last_index : index + 1])

# Append the last segment, including cases where the string ends with the separator
split_words.append(string[last_index:])
return split_words


Expand Down
Loading