diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..55b033e9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pytest \ No newline at end of file diff --git a/src/array_utils.py b/src/array_utils.py new file mode 100644 index 00000000..ee93261b --- /dev/null +++ b/src/array_utils.py @@ -0,0 +1,29 @@ +from typing import List, Union, Any + +def flatten(arr: Union[List[Any], Any]) -> List[Any]: + """ + Recursively flatten a nested list into a single-level list. + + Args: + arr (Union[List[Any], Any]): Input list that may contain nested lists. + + Returns: + List[Any]: A flattened list with all nested elements extracted. + + Raises: + TypeError: If input is not a list and cannot be flattened. + """ + # Base case: if input is not a list, return it as a single-element list + if not isinstance(arr, list): + return [arr] + + # Recursive flattening + flattened = [] + for item in arr: + # Recursively flatten each item + if isinstance(item, list): + flattened.extend(flatten(item)) + else: + flattened.append(item) + + 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..2e378e67 --- /dev/null +++ b/src/string_reversal.py @@ -0,0 +1,34 @@ +def reverse_string(input_string): + """ + Reverse the given string using a manual character-by-character approach. + + 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") + + # Convert string to list of characters + chars = list(input_string) + + # Reverse the list of characters manually + left = 0 + right = len(chars) - 1 + + while left < right: + # Swap characters + chars[left], chars[right] = chars[right], chars[left] + + # Move towards the center + 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_utils.py b/tests/test_array_utils.py new file mode 100644 index 00000000..49602f39 --- /dev/null +++ b/tests/test_array_utils.py @@ -0,0 +1,34 @@ +import pytest +from src.array_utils import flatten + +def test_flatten_simple_list(): + """Test flattening a simple list""" + assert flatten([1, 2, 3]) == [1, 2, 3] + +def test_flatten_nested_list(): + """Test flattening a list with one level of nesting""" + assert flatten([1, [2, 3], 4]) == [1, 2, 3, 4] + +def test_flatten_deeply_nested_list(): + """Test flattening a list with multiple levels of nesting""" + assert flatten([1, [2, [3, 4]], 5]) == [1, 2, 3, 4, 5] + +def test_flatten_complex_nested_list(): + """Test flattening a more complex nested list""" + assert flatten([1, [2, [3, [4, 5]]], 6]) == [1, 2, 3, 4, 5, 6] + +def test_flatten_empty_list(): + """Test flattening an empty list""" + assert flatten([]) == [] + +def test_flatten_non_list_single_element(): + """Test flattening a single non-list element""" + assert flatten(42) == [42] + +def test_flatten_mixed_types(): + """Test flattening a list with mixed types of nested elements""" + assert flatten([1, [2, 'three'], [4, [5, None]]]) == [1, 2, 'three', 4, 5, None] + +def test_nested_empty_lists(): + """Test flattening lists containing empty lists""" + assert flatten([1, [], [2, []], 3]) == [1, 2, 3] \ 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..da8abb11 --- /dev/null +++ b/tests/test_string_reversal.py @@ -0,0 +1,30 @@ +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_special_chars(): + """Test reversal of string with special characters.""" + assert reverse_string("a!b@c#") == "#c@b!a" + +def test_reverse_string_unicode(): + """Test reversal of unicode string.""" + assert reverse_string("café") == "éfac" + +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