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_reversal.cpython-312.pyc
Binary file not shown.
32 changes: 32 additions & 0 deletions src/string_reversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def reverse_string(input_string):
"""
Reverse a given string manually, without using slice notation or reverse().

Args:
input_string (str): The string to be reversed.

Returns:
str: The reversed string.

Raises:
TypeError: If 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, right = 0, len(chars) - 1
while left < right:
# Swap characters
chars[left], chars[right] = chars[right], chars[left]

# Move pointers
left += 1
right -= 1

# Convert back to string and return
return ''.join(chars)
Binary file not shown.
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_normal_string():
"""Test reversing a normal string."""
assert reverse_string("hello") == "olleh"

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

def test_reverse_single_char_string():
"""Test reversing a single character string."""
assert reverse_string("a") == "a"

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

def test_reverse_with_special_chars():
"""Test reversing a string with special characters."""
assert reverse_string("a!b@c#") == "#c@b!a"

def test_non_string_input():
"""Test that a 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"])