From 5ffd5aa71bf6ac7e55bc371d19e6ee441364365c Mon Sep 17 00:00:00 2001 From: RajdeepBakolia2004 <144157867+RajdeepBakolia2004@users.noreply.github.com> Date: Fri, 13 Dec 2024 19:30:57 +0530 Subject: [PATCH 1/4] fixed the issue in strings/join.py --- strings/join.py | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/strings/join.py b/strings/join.py index 5c02f65a20ce..1ce6a3a22edc 100644 --- a/strings/join.py +++ b/strings/join.py @@ -37,18 +37,43 @@ def join(separator: str, separated: list[str]) -> str: 'apple-banana-cherry' """ - joined = "" - for word_or_phrase in separated: - if not isinstance(word_or_phrase, str): + joined : str = "" + """ + The last element of the list is not followed by the separator. + So, we need to iterate through the list and join each element + with the separator except the last element. + """ + last_index : int = len(separated)-1 + """ + Iterate through the list and join each element with the separator. + Except the last element, all other elements are followed by the separator. + """ + for index in range(last_index): + """ + If the element is not a string, raise an exception. + """ + if not isinstance( separated[index] ,str): raise Exception("join() accepts only strings") - joined += word_or_phrase + separator - - # Remove the trailing separator - # by stripping it from the result - return joined.strip(separator) + """ + join the element with the separator. + """ + joined += separated[index] + separator + """ + If the list is not empty, join the last element. + """ + if (separated != []) : + """ + If the last element is not a string, raise an exception. + """ + if not isinstance(separated[len(separated)-1], str): + raise Exception("join() accepts only strings") + joined += separated[len(separated)-1] + """ + RETURN the joined string. + """ + return joined if __name__ == "__main__": from doctest import testmod - testmod() From 1be73e0c4742672d71e6bb25cd018d663ab6ca46 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 13 Dec 2024 14:07:45 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/join.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/strings/join.py b/strings/join.py index 1ce6a3a22edc..deecd02e9511 100644 --- a/strings/join.py +++ b/strings/join.py @@ -37,13 +37,13 @@ def join(separator: str, separated: list[str]) -> str: 'apple-banana-cherry' """ - joined : str = "" + joined: str = "" """ The last element of the list is not followed by the separator. So, we need to iterate through the list and join each element with the separator except the last element. """ - last_index : int = len(separated)-1 + last_index: int = len(separated) - 1 """ Iterate through the list and join each element with the separator. Except the last element, all other elements are followed by the separator. @@ -52,7 +52,7 @@ def join(separator: str, separated: list[str]) -> str: """ If the element is not a string, raise an exception. """ - if not isinstance( separated[index] ,str): + if not isinstance(separated[index], str): raise Exception("join() accepts only strings") """ join the element with the separator. @@ -61,13 +61,13 @@ def join(separator: str, separated: list[str]) -> str: """ If the list is not empty, join the last element. """ - if (separated != []) : + if separated != []: """ If the last element is not a string, raise an exception. """ - if not isinstance(separated[len(separated)-1], str): + if not isinstance(separated[len(separated) - 1], str): raise Exception("join() accepts only strings") - joined += separated[len(separated)-1] + joined += separated[len(separated) - 1] """ RETURN the joined string. """ @@ -76,4 +76,5 @@ def join(separator: str, separated: list[str]) -> str: if __name__ == "__main__": from doctest import testmod + testmod() From ae12344357016751c39544db1cdd759b5d904004 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sun, 29 Dec 2024 15:30:31 +0300 Subject: [PATCH 3/4] Update join.py --- strings/join.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/strings/join.py b/strings/join.py index deecd02e9511..6e053d962da2 100644 --- a/strings/join.py +++ b/strings/join.py @@ -24,6 +24,8 @@ def join(separator: str, separated: list[str]) -> str: 'a' >>> join(" ", ["You", "are", "amazing!"]) 'You are amazing!' + >>> join(",", ["", "", ""]) + ',,' This example should raise an exception for non-string elements: From aa97bb1ace90f5cd4967e612b061b2dbe9181286 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Sun, 29 Dec 2024 16:30:37 +0300 Subject: [PATCH 4/4] Update join.py --- strings/join.py | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/strings/join.py b/strings/join.py index 6e053d962da2..cdcc3a1377f4 100644 --- a/strings/join.py +++ b/strings/join.py @@ -39,6 +39,12 @@ def join(separator: str, separated: list[str]) -> str: 'apple-banana-cherry' """ + # Check that all elements are strings + for word_or_phrase in separated: + # If the element is not a string, raise an exception + if not isinstance(word_or_phrase, str): + raise Exception("join() accepts only strings") + joined: str = "" """ The last element of the list is not followed by the separator. @@ -50,29 +56,15 @@ def join(separator: str, separated: list[str]) -> str: Iterate through the list and join each element with the separator. Except the last element, all other elements are followed by the separator. """ - for index in range(last_index): - """ - If the element is not a string, raise an exception. - """ - if not isinstance(separated[index], str): - raise Exception("join() accepts only strings") - """ - join the element with the separator. - """ - joined += separated[index] + separator - """ - If the list is not empty, join the last element. - """ + for word_or_phrase in separated[:last_index]: + # join the element with the separator. + joined += word_or_phrase + separator + + # If the list is not empty, join the last element. if separated != []: - """ - If the last element is not a string, raise an exception. - """ - if not isinstance(separated[len(separated) - 1], str): - raise Exception("join() accepts only strings") - joined += separated[len(separated) - 1] - """ - RETURN the joined string. - """ + joined += separated[last_index] + + # Return the joined string. return joined