-
-
Notifications
You must be signed in to change notification settings - Fork 46k
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
base: master
Are you sure you want to change the base?
Changes from 8 commits
23a0f95
29b5cc2
8243480
376b637
f6c545f
de9aa38
0b45a6d
ae09865
fb63afe
b9d9cb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +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. | ||
|
||
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' | ||
Custom implementation of the join() function. | ||
This function manually concatenates the strings in the list, | ||
using the provided separator, without relying on str.join(). | ||
|
||
: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. | ||
|
||
: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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this if statement, with the removal of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for your advice. sure lemme check |
||
|
||
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 | ||
# Manually handle concatenation | ||
result = "" | ||
for i, element in enumerate(separated): | ||
result += element | ||
if i < len(separated) - 1: # Add separator only between elements | ||
result += separator | ||
|
||
# Remove the trailing separator | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Kaaserne can you please review it? |
||
# by stripping it from the result | ||
return joined.strip(separator) | ||
return result | ||
|
||
|
||
if __name__ == "__main__": | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason these got removed? I've never used
doctest
before but I think this has something to do with testing. Maybe you can also add: