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

Args:
input_str (str): The input 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_str, str):
raise TypeError("Input must be a string")

# Convert string to list of characters for easier manipulation
chars = list(input_str)

# Manually reverse the list of characters
left, right = 0, len(chars) - 1
while left < right:
# Swap characters from both ends
chars[left], chars[right] = chars[right], chars[left]
left += 1
right -= 1

# Convert back to string and return
return ''.join(chars)
Binary file not shown.
35 changes: 35 additions & 0 deletions tests/test_string_reversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import pytest
from src.string_reversal import reverse_string

def test_basic_string_reversal():
"""Test basic string reversal"""
assert reverse_string("hello") == "olleh"
assert reverse_string("python") == "nohtyp"

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

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

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

def test_string_with_special_characters():
"""Test reversal of string with special characters"""
assert reverse_string("!@#$%") == "%$#@!"

def test_unicode_string():
"""Test reversal of unicode string"""
assert reverse_string("こんにちは") == "はちにんこ"

def test_invalid_input_type():
"""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)