From 14aa5a603b97ebe59cb491728c54245e509658f4 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Mon, 5 May 2025 11:59:33 -0300 Subject: [PATCH 1/6] Add string reversal function implementation --- src/string_reversal.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/string_reversal.py diff --git a/src/string_reversal.py b/src/string_reversal.py new file mode 100644 index 00000000..b67c08f2 --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,19 @@ +def reverse_string(input_string: str) -> str: + """ + Reverse the given input string. + + Args: + input_string (str): The string to be reversed. + + Returns: + str: The reversed string. + + Raises: + TypeError: If the input is not a string. + """ + # Check if input is a string + if not isinstance(input_string, str): + raise TypeError("Input must be a string") + + # Return the reversed string + return input_string[::-1] \ No newline at end of file From 79a0c4cd893c45d8ca5111ce58183637eb28560a Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Mon, 5 May 2025 11:59:41 -0300 Subject: [PATCH 2/6] Add comprehensive tests for string reversal function --- tests/test_string_reversal.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/test_string_reversal.py diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py new file mode 100644 index 00000000..4eb741ef --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,34 @@ +import pytest +from src.string_reversal import reverse_string + +def test_reverse_string_basic(): + """Test basic string reversal.""" + assert reverse_string("hello") == "olleh" + assert reverse_string("python") == "nohtyp" + +def test_reverse_string_empty(): + """Test reversal of an empty string.""" + assert reverse_string("") == "" + +def test_reverse_string_single_char(): + """Test reversal of a single character.""" + assert reverse_string("a") == "a" + +def test_reverse_string_with_spaces(): + """Test reversal of string with spaces.""" + assert reverse_string("hello world") == "dlrow olleh" + +def test_reverse_string_with_special_chars(): + """Test reversal of string with special characters.""" + assert reverse_string("a1b2c3") == "3c2b1a" + +def test_reverse_string_invalid_input(): + """Test that TypeError is raised for non-string inputs.""" + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(123) + + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(None) + + with pytest.raises(TypeError, match="Input must be a string"): + reverse_string(["list"]) \ No newline at end of file From 7e305089455a81d63c54fb71355dee280f58a4d4 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Mon, 5 May 2025 12:00:30 -0300 Subject: [PATCH 3/6] Implement manual string reversal without slice notation --- src/string_reversal.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/string_reversal.py b/src/string_reversal.py index b67c08f2..a1331ec5 100644 --- a/src/string_reversal.py +++ b/src/string_reversal.py @@ -1,6 +1,6 @@ def reverse_string(input_string: str) -> str: """ - Reverse the given input string. + Reverse the given input string manually. Args: input_string (str): The string to be reversed. @@ -15,5 +15,22 @@ def reverse_string(input_string: str) -> str: if not isinstance(input_string, str): raise TypeError("Input must be a string") - # Return the reversed string - return input_string[::-1] \ No newline at end of file + # Handle empty string case + if len(input_string) <= 1: + return input_string + + # Convert string to list of characters for manual reversal + chars = list(input_string) + + # Manual reversal using two-pointer technique + left, right = 0, len(chars) - 1 + while left < right: + # Swap characters + chars[left], chars[right] = chars[right], chars[left] + + # Move pointers + left += 1 + right -= 1 + + # Convert back to string and return + return ''.join(chars) \ No newline at end of file From 62a28daabf8f5386e8dc1ab1e7d5e0cb59c2db3a Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Mon, 5 May 2025 12:00:38 -0300 Subject: [PATCH 4/6] Update tests for comprehensive string reversal coverage --- tests/test_string_reversal.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py index 4eb741ef..4f6df057 100644 --- a/tests/test_string_reversal.py +++ b/tests/test_string_reversal.py @@ -20,7 +20,11 @@ def test_reverse_string_with_spaces(): def test_reverse_string_with_special_chars(): """Test reversal of string with special characters.""" - assert reverse_string("a1b2c3") == "3c2b1a" + assert reverse_string("a1b2c3!@#") == "#@!3c2b1a" + +def test_reverse_string_mixed_characters(): + """Test reversal of string with mixed character types.""" + assert reverse_string("Hello, World! 123") == "321 !dlroW ,olleH" def test_reverse_string_invalid_input(): """Test that TypeError is raised for non-string inputs.""" From d2a56ac009f20c8342a20bfdae916321fd9a1a50 Mon Sep 17 00:00:00 2001 From: laura-abro Date: Mon, 5 May 2025 12:01:37 -0300 Subject: [PATCH 5/6] Implement array flattening function --- src/array_flattener.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/array_flattener.py diff --git a/src/array_flattener.py b/src/array_flattener.py new file mode 100644 index 00000000..bc81a6d7 --- /dev/null +++ b/src/array_flattener.py @@ -0,0 +1,34 @@ +from typing import List, Union + +def flatten_array(arr: List[Union[int, List]]) -> List[int]: + """ + Flatten a nested list of integers to a single-level list. + + Args: + arr (List[Union[int, List]]): A potentially nested list of integers. + + Returns: + List[int]: A flattened list of integers. + + Raises: + TypeError: If the input contains non-integer or non-list elements. + + Examples: + >>> flatten_array([1, [2, 3], 4]) + [1, 2, 3, 4] + >>> flatten_array([1, [2, [3, 4]], 5]) + [1, 2, 3, 4, 5] + """ + result = [] + + def _recursive_flatten(item): + if isinstance(item, int): + result.append(item) + elif isinstance(item, list): + for sub_item in item: + _recursive_flatten(sub_item) + else: + raise TypeError(f"Invalid element type: {type(item)}. Only integers and lists are allowed.") + + _recursive_flatten(arr) + return result \ No newline at end of file From 8aeef80dddcd2be3ab352b6860788c59b11f1ed8 Mon Sep 17 00:00:00 2001 From: laura-abro Date: Mon, 5 May 2025 12:01:44 -0300 Subject: [PATCH 6/6] Add comprehensive tests for array flattening function --- tests/test_array_flattener.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/test_array_flattener.py diff --git a/tests/test_array_flattener.py b/tests/test_array_flattener.py new file mode 100644 index 00000000..2c6938c5 --- /dev/null +++ b/tests/test_array_flattener.py @@ -0,0 +1,28 @@ +import pytest +from src.array_flattener import flatten_array + +def test_flatten_simple_nested_list(): + input_list = [1, [2, 3], 4] + assert flatten_array(input_list) == [1, 2, 3, 4] + +def test_flatten_deeply_nested_list(): + input_list = [1, [2, [3, 4]], 5] + assert flatten_array(input_list) == [1, 2, 3, 4, 5] + +def test_flatten_multiple_level_nesting(): + input_list = [1, [2, [3, [4, 5]]], 6] + assert flatten_array(input_list) == [1, 2, 3, 4, 5, 6] + +def test_flatten_empty_list(): + assert flatten_array([]) == [] + +def test_flatten_single_integer(): + assert flatten_array(5) == [5] + +def test_invalid_element_type(): + with pytest.raises(TypeError, match="Invalid element type"): + flatten_array([1, 2, "three"]) + +def test_mixed_nested_lists(): + input_list = [1, [2, 3, [4, [5]]], 6] + assert flatten_array(input_list) == [1, 2, 3, 4, 5, 6] \ No newline at end of file