From 1e3c15f448a4ad8dd971bc81da065ee2ebc27de4 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Thu, 10 Apr 2025 10:16:17 -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..3ea44edd --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,19 @@ +def reverse_string(s: str) -> str: + """ + Reverse the given string. + + Args: + s (str): The input 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(s, str): + raise TypeError("Input must be a string") + + # Reverse the string using slicing + return s[::-1] \ No newline at end of file From 9d5f832583bce0e8da071d73b7f3ccd4a3da3f0a Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Thu, 10 Apr 2025 10:16:28 -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..0fdf65aa --- /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 reversing an empty string.""" + assert reverse_string("") == "" + +def test_reverse_string_single_char(): + """Test reversing a single character.""" + assert reverse_string("a") == "a" + +def test_reverse_string_with_spaces(): + """Test reversing a string with spaces.""" + assert reverse_string("hello world") == "dlrow olleh" + +def test_reverse_string_with_special_chars(): + """Test reversing a string with special characters.""" + assert reverse_string("a1b2c3!@#") == "#@!3c2b1a" + +def test_reverse_string_type_error(): + """Test that a 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 8a733aa69229bf592a52f3a56567ce05f4acfa11 Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Thu, 10 Apr 2025 10:17:09 -0300 Subject: [PATCH 3/6] Implement manual string reversal without using slice or reverse() --- src/string_reversal.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/string_reversal.py b/src/string_reversal.py index 3ea44edd..2fa45ae9 100644 --- a/src/string_reversal.py +++ b/src/string_reversal.py @@ -1,6 +1,6 @@ def reverse_string(s: str) -> str: """ - Reverse the given string. + Reverse the given string manually, character by character. Args: s (str): The input string to be reversed. @@ -11,9 +11,22 @@ def reverse_string(s: str) -> str: Raises: TypeError: If the input is not a string. """ - # Check if input is a string + # Type checking if not isinstance(s, str): raise TypeError("Input must be a string") - # Reverse the string using slicing - return s[::-1] \ No newline at end of file + # Manual string reversal using list conversion and iteration + # Convert string to list of characters + chars = list(s) + + # Use two-pointer technique to swap characters + 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 0201d01335233f78481ec8317eb769171fd08f9a Mon Sep 17 00:00:00 2001 From: labrocadabro Date: Thu, 10 Apr 2025 10:17:17 -0300 Subject: [PATCH 4/6] Update test cases for string reversal function --- tests/test_string_reversal.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py index 0fdf65aa..1f2d46a4 100644 --- a/tests/test_string_reversal.py +++ b/tests/test_string_reversal.py @@ -22,6 +22,14 @@ def test_reverse_string_with_special_chars(): """Test reversing a string with special characters.""" assert reverse_string("a1b2c3!@#") == "#@!3c2b1a" +def test_reverse_string_palindrome(): + """Test reversing a palindrome.""" + assert reverse_string("racecar") == "racecar" + +def test_reverse_string_mixed_case(): + """Test reversing a string with mixed case.""" + assert reverse_string("HeLLo") == "oLLeH" + def test_reverse_string_type_error(): """Test that a TypeError is raised for non-string inputs.""" with pytest.raises(TypeError, match="Input must be a string"): From 9054c549b6188dccc40328265eea15e88ab69e4c Mon Sep 17 00:00:00 2001 From: laura-abro Date: Thu, 10 Apr 2025 10:18:17 -0300 Subject: [PATCH 5/6] Add array flattening function implementation --- src/array_flattener.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 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..92cb62ae --- /dev/null +++ b/src/array_flattener.py @@ -0,0 +1,41 @@ +from typing import List, Union + +def flatten_array(arr: List[Union[int, List]]) -> List[int]: + """ + Recursively flatten a nested array of integers. + + This function takes a potentially nested list and returns a + flat list containing all integer elements from the original list. + + Args: + arr (List[Union[int, List]]): A potentially nested list of integers. + + Returns: + List[int]: A flattened list containing all integer elements. + + Examples: + >>> flatten_array([1, [2, 3], 4]) + [1, 2, 3, 4] + >>> flatten_array([1, [2, [3, 4]], 5]) + [1, 2, 3, 4, 5] + >>> flatten_array([]) + [] + """ + # Handle base cases + if not arr: + return [] + + # Recursive flattening + flattened = [] + for item in arr: + # If the item is a list, recursively flatten it + if isinstance(item, list): + flattened.extend(flatten_array(item)) + # If the item is an integer, add it to the list + elif isinstance(item, int): + flattened.append(item) + # Ignore other types + else: + continue + + return flattened \ No newline at end of file From b97222d3d41b7aa62dd3493ef04c83da1cc2d551 Mon Sep 17 00:00:00 2001 From: laura-abro Date: Thu, 10 Apr 2025 10:18:26 -0300 Subject: [PATCH 6/6] Add comprehensive tests for array flattening function --- tests/test_array_flattener.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 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..67208fbd --- /dev/null +++ b/tests/test_array_flattener.py @@ -0,0 +1,34 @@ +import pytest +from src.array_flattener import flatten_array + +def test_flatten_simple_list(): + """Test flattening a simple list with nested elements.""" + assert flatten_array([1, [2, 3], 4]) == [1, 2, 3, 4] + +def test_flatten_deeply_nested_list(): + """Test flattening a deeply nested list.""" + assert flatten_array([1, [2, [3, 4]], 5]) == [1, 2, 3, 4, 5] + +def test_flatten_empty_list(): + """Test flattening an empty list.""" + assert flatten_array([]) == [] + +def test_flatten_list_with_empty_sublists(): + """Test flattening a list with empty sublists.""" + assert flatten_array([1, [], [2, []], 3]) == [1, 2, 3] + +def test_flatten_multiple_levels_of_nesting(): + """Test flattening multiple levels of nested lists.""" + assert flatten_array([1, [2, [3, [4]]], 5]) == [1, 2, 3, 4, 5] + +def test_flatten_list_with_non_integer_elements(): + """Test flattening a list with non-integer elements (should ignore them).""" + assert flatten_array([1, [2, 'a', 3], 4.5, [5]]) == [1, 2, 3, 5] + +def test_flatten_single_element_list(): + """Test flattening a single-element list.""" + assert flatten_array([42]) == [42] + +def test_flatten_complex_nested_structure(): + """Test flattening a complex nested structure.""" + assert flatten_array([1, [2, [3, [4, [5]]], 6], 7]) == [1, 2, 3, 4, 5, 6, 7] \ No newline at end of file