diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..55b033e --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pytest \ No newline at end of file diff --git a/src/__pycache__/string_utils.cpython-312.pyc b/src/__pycache__/string_utils.cpython-312.pyc new file mode 100644 index 0000000..551f20b Binary files /dev/null and b/src/__pycache__/string_utils.cpython-312.pyc differ diff --git a/src/array_utils.py b/src/array_utils.py new file mode 100644 index 0000000..594c283 --- /dev/null +++ b/src/array_utils.py @@ -0,0 +1,31 @@ +from typing import List, Union, Any + +def flatten_array(arr: Union[List, Any]) -> List: + """ + Recursively flatten a nested list into a single-level list. + + Args: + arr (Union[List, Any]): Input list or nested list to be flattened. + + Returns: + List: A flattened list containing all non-list elements. + + Examples: + >>> flatten_array([1, [2, 3], [4, [5, 6]]]) + [1, 2, 3, 4, 5, 6] + >>> flatten_array([1, 2, 3]) + [1, 2, 3] + >>> flatten_array([]) + [] + """ + # Base case: if input is not a list, return it as a single-element list + if not isinstance(arr, list): + return [arr] + + # Recursive case: flatten each element and combine + flattened = [] + for item in arr: + # Recursively flatten each item and extend the result + flattened.extend(flatten_array(item)) + + return flattened \ No newline at end of file diff --git a/src/string_utils.py b/src/string_utils.py new file mode 100644 index 0000000..9cb330e --- /dev/null +++ b/src/string_utils.py @@ -0,0 +1,34 @@ +def reverse_string(input_string): + """ + Reverse the given string using a manual character-by-character reversal algorithm. + + 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 for manual reversal + chars = list(input_string) + + # Manual reversal using two-pointer technique + left = 0 + right = len(chars) - 1 + + while left < right: + # Swap characters + chars[left], chars[right] = chars[right], chars[left] + + # Move pointers towards center + left += 1 + right -= 1 + + # Convert back to string and return + return ''.join(chars) \ No newline at end of file diff --git a/tests/__pycache__/test_string_utils.cpython-312-pytest-8.3.5.pyc b/tests/__pycache__/test_string_utils.cpython-312-pytest-8.3.5.pyc new file mode 100644 index 0000000..102a9f5 Binary files /dev/null and b/tests/__pycache__/test_string_utils.cpython-312-pytest-8.3.5.pyc differ diff --git a/tests/test_array_utils.py b/tests/test_array_utils.py new file mode 100644 index 0000000..4e96f5f --- /dev/null +++ b/tests/test_array_utils.py @@ -0,0 +1,30 @@ +import pytest +from src.array_utils import flatten_array + +def test_flatten_simple_list(): + """Test flattening a simple list.""" + assert flatten_array([1, 2, 3]) == [1, 2, 3] + +def test_flatten_nested_list(): + """Test flattening a nested list.""" + assert flatten_array([1, [2, 3], [4, [5, 6]]]) == [1, 2, 3, 4, 5, 6] + +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_mixed_content(): + """Test flattening a list with mixed content types.""" + assert flatten_array([1, 'a', [2, ['b']], 3]) == [1, 'a', 2, 'b', 3] + +def test_flatten_single_item(): + """Test flattening a single item.""" + assert flatten_array(42) == [42] + +def test_flatten_nested_empty_lists(): + """Test flattening list with nested empty lists.""" + assert flatten_array([[], [1, []], 2]) == [1, 2] \ No newline at end of file diff --git a/tests/test_string_utils.py b/tests/test_string_utils.py new file mode 100644 index 0000000..63ba350 --- /dev/null +++ b/tests/test_string_utils.py @@ -0,0 +1,37 @@ +import pytest +from src.string_utils import reverse_string + +def test_reverse_standard_string(): + """Test reversing a standard ASCII string.""" + assert reverse_string("hello") == "olleh" + assert reverse_string("python") == "nohtyp" + +def test_reverse_empty_string(): + """Test reversing an empty string.""" + assert reverse_string("") == "" + +def test_reverse_unicode_string(): + """Test reversing a string with unicode characters.""" + assert reverse_string("こんにちは") == "はちにんこ" + assert reverse_string("Hello, 世界!") == "!界世 ,olleH" + +def test_reverse_palindrome(): + """Test reversing a palindrome.""" + assert reverse_string("racecar") == "racecar" + assert reverse_string("12321") == "12321" + +def test_reverse_with_spaces_and_punctuation(): + """Test reversing strings with spaces and punctuation.""" + assert reverse_string("hello world") == "dlrow olleh" + assert reverse_string("a,b,c") == "c,b,a" + +def test_invalid_input_type(): + """Test that non-string inputs raise a TypeError.""" + with pytest.raises(TypeError): + reverse_string(12345) + + with pytest.raises(TypeError): + reverse_string(None) + + with pytest.raises(TypeError): + reverse_string(["hello"]) \ No newline at end of file