Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pytest
29 changes: 29 additions & 0 deletions src/array_utils.py
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions src/string_reversal.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 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)
34 changes: 34 additions & 0 deletions tests/test_array_utils.py
Original file line number Diff line number Diff line change
@@ -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]
30 changes: 30 additions & 0 deletions tests/test_string_reversal.py
Original file line number Diff line number Diff line change
@@ -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"])