From 15299caf5da424f167848075dfc47b77bca8e2e1 Mon Sep 17 00:00:00 2001 From: Madiha Malikzada Date: Fri, 27 Dec 2024 00:09:08 +0530 Subject: [PATCH 1/4] Write the solution for the name shuffler challange --- solutions/abcd.py | 51 +++++++++++++++++++++++++-- solutions/name_shuffler.py | 35 ++++++++++++++++++ solutions/tests/test_name_shuffler.py | 23 ++++++++++++ 3 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 solutions/name_shuffler.py create mode 100644 solutions/tests/test_name_shuffler.py diff --git a/solutions/abcd.py b/solutions/abcd.py index 739dfd80f..6cf178765 100644 --- a/solutions/abcd.py +++ b/solutions/abcd.py @@ -1,2 +1,49 @@ -# this is an example file -print("example") +def romanToInt( string): + """ + This function will take a roman number as an input and coverts it to an integr and returns it + + :type s: str + :rtype: int + + inputs: + str: a string of roman numbers that we have to covert + + output: + int: return the converted integer + + raises: + type error if input is not a str + + examples: + >>> romanToInt('I') + 1 + >>> romanToInt('V') + 5 + + >>> romanToInt('IV') + 4 + >>> romanToInt('X') + 10 + + >>> romanToInt('XL') + 40 + """ + + if string == 'I': + return 1 + + if string == 'III': + return 3 + + list + count = 0 + result = [string[i:i+2] for i in range(0, len(string), 2)] + + return result + + + +print(romanToInt("IV")) + + + \ No newline at end of file diff --git a/solutions/name_shuffler.py b/solutions/name_shuffler.py new file mode 100644 index 000000000..f4301f8e9 --- /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), "Input should be a str" + + splitStr = string.split(' ') + + reverseStr = splitStr[::-1] + return ' '.join(reverseStr) + + +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..8a2f61078 --- /dev/null +++ b/solutions/tests/test_name_shuffler.py @@ -0,0 +1,23 @@ +import unittest + +from solutions.name_shuffler import name_shuffler + + +class TestNameShuffler(unittest.TestCase): + + def test_only_first_name_input(self): + """Test first input""" + self.assertEqual(name_shuffler('Mojtaba'), 'Mojtaba') + + + def test_fatima_input(self): + self.assertEqual(name_shuffler('Fatima Malik'), 'Malik Fatima') + + + def test_long_name_input(self): + self.assertEqual(name_shuffler(''), '') + + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file From d53298bb51c4df3cf7165763c0831c09456ce909 Mon Sep 17 00:00:00 2001 From: Madiha Malikzada Date: Fri, 27 Dec 2024 03:35:03 +0530 Subject: [PATCH 2/4] delete the abcd.py file --- solutions/abcd.py | 49 ----------------------------------------------- 1 file changed, 49 deletions(-) delete mode 100644 solutions/abcd.py diff --git a/solutions/abcd.py b/solutions/abcd.py deleted file mode 100644 index 6cf178765..000000000 --- a/solutions/abcd.py +++ /dev/null @@ -1,49 +0,0 @@ -def romanToInt( string): - """ - This function will take a roman number as an input and coverts it to an integr and returns it - - :type s: str - :rtype: int - - inputs: - str: a string of roman numbers that we have to covert - - output: - int: return the converted integer - - raises: - type error if input is not a str - - examples: - >>> romanToInt('I') - 1 - >>> romanToInt('V') - 5 - - >>> romanToInt('IV') - 4 - >>> romanToInt('X') - 10 - - >>> romanToInt('XL') - 40 - """ - - if string == 'I': - return 1 - - if string == 'III': - return 3 - - list - count = 0 - result = [string[i:i+2] for i in range(0, len(string), 2)] - - return result - - - -print(romanToInt("IV")) - - - \ No newline at end of file From 25bda2319cc9310d09d4145a777c77647780397c Mon Sep 17 00:00:00 2001 From: Madiha Malikzada Date: Sat, 28 Dec 2024 01:50:05 +0530 Subject: [PATCH 3/4] added four spaces intstead of tabs to address ci errors --- solutions/name_shuffler.py | 4 +++- solutions/tests/test_name_shuffler.py | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/solutions/name_shuffler.py b/solutions/name_shuffler.py index 707aeb1c5..84dc0da0c 100644 --- a/solutions/name_shuffler.py +++ b/solutions/name_shuffler.py @@ -23,11 +23,13 @@ """ 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 index 4d525feac..dfdc1c11e 100644 --- a/solutions/tests/test_name_shuffler.py +++ b/solutions/tests/test_name_shuffler.py @@ -27,3 +27,6 @@ def test_long_name_input(self): if __name__ == '__main__': unittest.main() + + +# run test: python3 -m unittest solutions.tests.test_name_shuffler \ No newline at end of file From 68387953862e0481b0b6bed732c736a9bff39fe5 Mon Sep 17 00:00:00 2001 From: Madiha Malikzada Date: Sat, 28 Dec 2024 03:19:02 +0530 Subject: [PATCH 4/4] formated the code --- solutions/tests/test_name_shuffler.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/solutions/tests/test_name_shuffler.py b/solutions/tests/test_name_shuffler.py index dfdc1c11e..c819b340d 100644 --- a/solutions/tests/test_name_shuffler.py +++ b/solutions/tests/test_name_shuffler.py @@ -1,8 +1,9 @@ +import unittest + import sys print("sys.path:", sys.path) -import unittest -from solutions.name_shuffler import name_shuffler +from ..name_shuffler import name_shuffler # Test Cases for the name_shuffler function class TestNameShuffler(unittest.TestCase):