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
Binary file added src/__pycache__/string_utils.cpython-310.pyc
Binary file not shown.
33 changes: 33 additions & 0 deletions src/string_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def reverse_string(input_string: str) -> str:
"""
Reverses the given input 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")

# Handle empty string case
if len(input_string) <= 1:
return input_string

# Convert string to list for manipulation
chars = list(input_string)

# Use two-pointer technique to swap characters
left, right = 0, len(chars) - 1
while left < right:
chars[left], chars[right] = chars[right], chars[left]
left += 1
right -= 1

# Convert back to string
return ''.join(chars)
Binary file not shown.
42 changes: 42 additions & 0 deletions tests/test_string_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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("world") == "dlrow"

def test_reverse_string_empty():
"""Test reversal of an empty string."""
assert reverse_string("") == ""

def test_reverse_string_single_char():
"""Test reversal of a single character."""
assert reverse_string("a") == "a"

def test_reverse_string_with_spaces():
"""Test reversal of string with spaces."""
assert reverse_string("hello world") == "dlrow olleh"

def test_reverse_string_with_special_chars():
"""Test reversal of string with special characters."""
assert reverse_string("Hello, World!") == "!dlroW ,olleH"

def test_reverse_string_with_unicode():
"""Test reversal of string with unicode characters."""
assert reverse_string("こんにちは") == "はちにんこ"

def test_reverse_string_with_numbers():
"""Test reversal of string with numbers."""
assert reverse_string("123 abc") == "cba 321"

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(12345)

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(["hello"])