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 diff --git a/src/string_reversal.py b/src/string_reversal.py new file mode 100644 index 00000000..a1331ec5 --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,36 @@ +def reverse_string(input_string: str) -> str: + """ + Reverse the given input string manually. + + 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") + + # 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 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 diff --git a/tests/test_string_reversal.py b/tests/test_string_reversal.py new file mode 100644 index 00000000..4f6df057 --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,38 @@ +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_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.""" + 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