diff --git a/solutions/name_shuffler.py b/solutions/name_shuffler.py new file mode 100644 index 000000000..84dc0da0c --- /dev/null +++ b/solutions/name_shuffler.py @@ -0,0 +1,35 @@ +""" +This function returns a string in which the first name is swapped with the last name. + +parameters: +str: A string of name that we need swap + +Output: +str: the swapped input + +Raises: +TypeError: if the input is not a str + +Examples: +>>> name_shuffler('') +'' + +>>> name_shuffler('Mojtaba') +'Mojtaba" + +>>> name_shuffler('Fatima Malik') +"Malik Fatima" + +""" + +def name_shuffler(string: str): + + assert isinstance(string, str) and string.strip(), "Input must be a non-empty string" + + split_str = string.split(' ') + + reverse_str = split_str[::-1] + + return ' '.join(reverse_str) + +name_shuffler('Mojtaba') \ No newline at end of file diff --git a/solutions/tests/test_name_shuffler.py b/solutions/tests/test_name_shuffler.py new file mode 100644 index 000000000..c819b340d --- /dev/null +++ b/solutions/tests/test_name_shuffler.py @@ -0,0 +1,33 @@ +import unittest + +import sys +print("sys.path:", sys.path) + +from ..name_shuffler import name_shuffler + +# Test Cases for the name_shuffler function +class TestNameShuffler(unittest.TestCase): + + def test_empty_input(self): + """Test empty input raises an error""" + with self.assertRaises(AssertionError) as context: + name_shuffler('') + self.assertEqual(str(context.exception), "Input must be a non-empty string") + + def test_only_first_name_input(self): + """Test input with a single first name only""" + self.assertEqual(name_shuffler('Mojtaba'), 'Mojtaba') + + def test_full_name_input(self): + """Test input with a first and last name""" + self.assertEqual(name_shuffler('Fatima Malik'), 'Malik Fatima') + + def test_long_name_input(self): + """Test input with a long name""" + self.assertEqual(name_shuffler('Fatima Zohra Malik'), 'Malik Zohra Fatima') + +if __name__ == '__main__': + unittest.main() + + +# run test: python3 -m unittest solutions.tests.test_name_shuffler \ No newline at end of file