From c21fc372f740cbda86166cae98f5171b5d2707d3 Mon Sep 17 00:00:00 2001 From: Saurabh Mahapatra <98408932+its-100rabh@users.noreply.github.com> Date: Tue, 17 Oct 2023 08:38:05 +0530 Subject: [PATCH 1/2] Added test cases to join.py --- strings/join.py | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/strings/join.py b/strings/join.py index 739856c1aa93..b4c0e0920115 100644 --- a/strings/join.py +++ b/strings/join.py @@ -1,10 +1,20 @@ """ -Program to join a list of strings with a given separator +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"]) @@ -13,20 +23,29 @@ def join(separator: str, separated: list[str]) -> str: '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 to be joined + Exception: join() accepts only strings + + 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 to be joined") + 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) - if __name__ == "__main__": from doctest import testmod - testmod() From d712d67bdd7f534fad111e7d75a8eecb6e1d6a33 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Oct 2023 03:09:34 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- strings/join.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/strings/join.py b/strings/join.py index b4c0e0920115..5c02f65a20ce 100644 --- a/strings/join.py +++ b/strings/join.py @@ -2,12 +2,13 @@ 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 + Joins a list of strings using a separator and returns the result. - :param separator: Separator to be used + :param separator: Separator to be used for joining the strings. :param separated: List of strings to be joined. @@ -23,8 +24,8 @@ def join(separator: str, separated: list[str]) -> str: 'a' >>> join(" ", ["You", "are", "amazing!"]) 'You are amazing!' - - This example should raise an + + This example should raise an exception for non-string elements: >>> join("#", ["a", "b", "c", 1]) Traceback (most recent call last): @@ -35,17 +36,19 @@ def join(separator: str, separated: list[str]) -> str: >>> 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 - - # Remove the trailing separator + + # Remove the trailing separator # by stripping it from the result return joined.strip(separator) + if __name__ == "__main__": from doctest import testmod + testmod()