-
-
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 6 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 |
---|---|---|
|
@@ -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. | ||
|
@@ -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): | ||
... | ||
|
@@ -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): | ||
raise Exception("join() accepts only strings") | ||
|
||
joined = separator.join(separated) | ||
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. 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? 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. 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() |
||
|
||
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? |
||
# Remove the trailing separator | ||
# by stripping it from the result | ||
return joined.strip(separator) | ||
return joined | ||
|
||
|
||
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.
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.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.
Thank you for your advice. sure lemme check