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 diff --git a/src/string_reversal.py b/src/string_reversal.py new file mode 100644 index 00000000..2fa45ae9 --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,32 @@ +def reverse_string(s: str) -> str: + """ + Reverse the given string manually, character by character. + + Args: + s (str): The input string to be reversed. + + Returns: + str: The reversed string. + + Raises: + TypeError: If the input is not a string. + """ + # Type checking + if not isinstance(s, str): + raise TypeError("Input must be a string") + + # 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 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 diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py new file mode 100644 index 00000000..1f2d46a4 --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,42 @@ +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_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"): + 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