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
Binary file added src/__pycache__/string_utils.cpython-312.pyc
Binary file not shown.
26 changes: 26 additions & 0 deletions src/string_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def reverse_string(input_string):
"""
Reverse a 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.
"""
# Validate input is a string
if not isinstance(input_string, str):
raise TypeError("Input must be a string")

# Use a list to build the reversed string
reversed_chars = []

# Iterate through the string from end to beginning
for i in range(len(input_string) - 1, -1, -1):
reversed_chars.append(input_string[i])

# Convert list of characters back to string
return ''.join(reversed_chars)
Binary file not shown.
32 changes: 32 additions & 0 deletions tests/test_string_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest
from src.string_utils 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 with special characters and spaces."""
assert reverse_string("Hello, World!") == "!dlroW ,olleH"
assert reverse_string(" trim ") == " mirt "

def test_reverse_string_unicode():
"""Test reversal with Unicode characters."""
assert reverse_string("こんにちは") == "はちにんこ"
assert reverse_string("🌈🦄") == "🦄🌈"

def test_reverse_string_invalid_input():
"""Test error handling 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"])