Skip to content
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

Write the solution for the name shuffler challange #20

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions solutions/name_shuffler.py
Original file line number Diff line number Diff line change
@@ -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')
33 changes: 33 additions & 0 deletions solutions/tests/test_name_shuffler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import unittest

import sys
print("sys.path:", sys.path)

from ..name_shuffler import name_shuffler

Check failure on line 6 in solutions/tests/test_name_shuffler.py

View workflow job for this annotation

GitHub Actions / py_linting

Ruff (E402)

solutions/tests/test_name_shuffler.py:6:1: E402 Module level import not at top of file

Check failure on line 6 in solutions/tests/test_name_shuffler.py

View workflow job for this annotation

GitHub Actions / py_linting

Ruff (E402)

solutions/tests/test_name_shuffler.py:6:1: E402 Module level import not at top of file

# 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
Loading