From fdc1af4551e4ac34564d36c6dea3c6b7520897c6 Mon Sep 17 00:00:00 2001 From: Tibyan Khalid Date: Thu, 9 Jan 2025 20:13:16 +0200 Subject: [PATCH 1/2] Swap_letters challenge and tests --- solutions/Swap_letters.py | 37 +++++++++++++++++++++++++++ solutions/tests/test_Swap_letters.py | 38 ++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 solutions/Swap_letters.py create mode 100644 solutions/tests/test_Swap_letters.py diff --git a/solutions/Swap_letters.py b/solutions/Swap_letters.py new file mode 100644 index 000000000..5800e6f16 --- /dev/null +++ b/solutions/Swap_letters.py @@ -0,0 +1,37 @@ +""" +Created on 08/01/2024 + +@author: Tibyan Khalid +""" + + +def Swap_letters(string: str) -> str: + """Swap_letters will return the string given with switched cases. + + Parameters: + String(str) "the word that will get modified" + + Returns: modified_string(str) "the string with swapped cases" + + >>> Swap_letters("hello") + 'HELLO' + + >>> Swap_letters("HELLO") + 'hello' + + >>> Swap_letters("HeLlO") + 'hElLo' + """ + if not isinstance(string, str): + "Make sure the expected input is a string" + raise AssertionError("Input must be a string") + + changed_string = "" + for char in string: + if char.islower(): + changed_string = changed_string + char.upper() + elif char.isupper(): + changed_string = changed_string + char.lower() + else: + changed_string = changed_string + char + return changed_string diff --git a/solutions/tests/test_Swap_letters.py b/solutions/tests/test_Swap_letters.py new file mode 100644 index 000000000..fec2f57be --- /dev/null +++ b/solutions/tests/test_Swap_letters.py @@ -0,0 +1,38 @@ +""" +Created on 08/01/2024 + +@author: Tibyan Khalid +""" + +import unittest + +from ..Swap_letters import Swap_letters + + +class TestSwapletters(unittest.TestCase): + """Unittests for the Swap_letters function""" + + def test_lowercase_all(self): + "Testing from lowercase to uppercase" + self.assertEqual(Swap_letters("tibyan"), "TIBYAN") + + def test_uppercase_all(self): + "Testing from uppercase to lowercase" + self.assertEqual(Swap_letters("TIBYAN"), "tibyan") + + def test_mixed_cases(self): + "Testing mixed spaces" + self.assertEqual(Swap_letters("TiByAn"), "tIbYaN") + + def test_non_string_entry(self): + "Raise an error if entry is not a string" + with self.assertRaises(AssertionError): + Swap_letters(57) + + def test_spaces(self): + "Handle spaces correctly" + self.assertEqual(Swap_letters("Hello World"), "hELLO wORLD") + + +if __name__ == "__main__": + unittest.main() From c3d22d01c9cda80abcdf82ed04f4e7c694ab008a Mon Sep 17 00:00:00 2001 From: Tibyan Khalid Date: Sun, 12 Jan 2025 09:29:10 +0200 Subject: [PATCH 2/2] Added changes as per request of the reveiwer such as including edge cases tests and more descriptive docstrings --- solutions/Swap_letters.py | 8 +++++--- solutions/tests/test_Swap_letters.py | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/solutions/Swap_letters.py b/solutions/Swap_letters.py index 5800e6f16..05a21bf18 100644 --- a/solutions/Swap_letters.py +++ b/solutions/Swap_letters.py @@ -2,6 +2,8 @@ Created on 08/01/2024 @author: Tibyan Khalid + +This file contains the function Swap_letters that swaps the case of each letter in a string. """ @@ -29,9 +31,9 @@ def Swap_letters(string: str) -> str: changed_string = "" for char in string: if char.islower(): - changed_string = changed_string + char.upper() + changed_string += char.upper() elif char.isupper(): - changed_string = changed_string + char.lower() + changed_string += char.lower() else: - changed_string = changed_string + char + changed_string += char return changed_string diff --git a/solutions/tests/test_Swap_letters.py b/solutions/tests/test_Swap_letters.py index fec2f57be..f4cb40d90 100644 --- a/solutions/tests/test_Swap_letters.py +++ b/solutions/tests/test_Swap_letters.py @@ -2,6 +2,8 @@ Created on 08/01/2024 @author: Tibyan Khalid + +This file contains boundary cases and defensive assertion tests for the function Swap_letters. """ import unittest @@ -9,8 +11,9 @@ from ..Swap_letters import Swap_letters -class TestSwapletters(unittest.TestCase): - """Unittests for the Swap_letters function""" +class TestSwapLettersFunctionality(unittest.TestCase): + """Test cases for validating the behavior of the Swap_letters function, + including edge cases and defensive assertions.""" def test_lowercase_all(self): "Testing from lowercase to uppercase" @@ -33,6 +36,14 @@ def test_spaces(self): "Handle spaces correctly" self.assertEqual(Swap_letters("Hello World"), "hELLO wORLD") + def test_empty_string(self): + "Test for an empty string input" + self.assertEqual(Swap_letters(""), "") + + def test_special_characters(self): + "Test for special characters input" + self.assertEqual(Swap_letters("1234!@#$"), "1234!@#$") + if __name__ == "__main__": unittest.main()