Skip to content
Open
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest
Binary file added src/__pycache__/string_utils.cpython-312.pyc
Binary file not shown.
31 changes: 31 additions & 0 deletions src/array_utils.py
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions src/string_utils.py
Original file line number Diff line number Diff line change
@@ -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)
Binary file not shown.
30 changes: 30 additions & 0 deletions tests/test_array_utils.py
Original file line number Diff line number Diff line change
@@ -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]
37 changes: 37 additions & 0 deletions tests/test_string_utils.py
Original file line number Diff line number Diff line change
@@ -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"])