Skip to content
36 changes: 36 additions & 0 deletions src/array_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from typing import List, Union, Any

def flatten_array(arr: Union[List[Any], Any]) -> List[Any]:
"""
Recursively flatten a nested array of arbitrary depth into a single-level list.

Args:
arr (Union[List[Any], Any]): The input array to be flattened.
Can be a nested list or a single element.

Returns:
List[Any]: A flattened list containing all elements from the input.

Examples:
>>> flatten_array([1, [2, 3], [4, [5, 6]]])
[1, 2, 3, 4, 5, 6]
>>> flatten_array([])
[]
>>> flatten_array(1)
[1]
"""
# Base case: if input is not a list, return it as a single-element list
if not isinstance(arr, list):
return [arr]

# If list is empty, return empty list
if not arr:
return []

# Recursive flattening
flattened = []
for item in arr:
# Recursively flatten each item and extend the result
flattened.extend(flatten_array(item))

return flattened
23 changes: 23 additions & 0 deletions src/string_reversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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")

# Manual string reversal using list conversion and iteration
reversed_chars = []
for char in input_string:
reversed_chars.insert(0, char)

return ''.join(reversed_chars)
31 changes: 31 additions & 0 deletions tests/test_array_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest
from src.array_utils import flatten_array

def test_flatten_simple_nested_array():
"""Test flattening a simply nested array."""
assert flatten_array([1, [2, 3], 4]) == [1, 2, 3, 4]

def test_flatten_deeply_nested_array():
"""Test flattening a deeply nested array."""
assert flatten_array([1, [2, [3, 4]], [5, [6, 7]]]) == [1, 2, 3, 4, 5, 6, 7]

def test_flatten_empty_array():
"""Test flattening an empty array."""
assert flatten_array([]) == []

def test_flatten_single_element():
"""Test flattening a single non-list element."""
assert flatten_array(5) == [5]

def test_flatten_mixed_types_array():
"""Test flattening an array with mixed types and nesting."""
assert flatten_array([1, 'a', [2, [3.14]], [True, []]]) == [1, 'a', 2, 3.14, True]

def test_flatten_nested_empty_arrays():
"""Test flattening an array with nested empty arrays."""
assert flatten_array([1, [], [2, []], 3]) == [1, 2, 3]

def test_flatten_multiple_level_nesting():
"""Test flattening an array with multiple levels of nesting."""
nested_array = [1, [2, [3, [4, [5]]]], 6]
assert flatten_array(nested_array) == [1, 2, 3, 4, 5, 6]
33 changes: 33 additions & 0 deletions tests/test_string_reversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest
from src.string_reversal import reverse_string

def test_reverse_standard_string():
"""Test reversing a standard string."""
assert reverse_string("hello") == "olleh"

def test_reverse_empty_string():
"""Test reversing an empty string."""
assert reverse_string("") == ""

def test_reverse_palindrome():
"""Test reversing a palindrome."""
assert reverse_string("racecar") == "racecar"

def test_reverse_with_spaces():
"""Test reversing a string with spaces."""
assert reverse_string("hello world") == "dlrow olleh"

def test_reverse_with_special_characters():
"""Test reversing a string with special characters."""
assert reverse_string("a1b2c3!@#") == "#@!3c2b1a"

def test_reverse_unicode_string():
"""Test reversing a string with unicode characters."""
assert reverse_string("こんにちは") == "はちにんこ"

def test_invalid_input_type():
"""Test that a TypeError is raised for non-string input."""
with pytest.raises(TypeError, match="Input must be a string"):
reverse_string(12345)
reverse_string(None)
reverse_string(["list"])