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 modified src/__pycache__/string_reversal.cpython-312.pyc
Binary file not shown.
Binary file modified src/__pycache__/string_utils.cpython-312.pyc
Binary file not shown.
13 changes: 9 additions & 4 deletions src/string_reversal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
def reverse_string(input_string):
def reverse_string(input_string: str) -> str:
"""
Reverse the given input string manually, without using slice notation or reverse().
Reverse the given input string manually, without using slice notation or built-in reverse methods.

This implementation provides a robust, type-hinted string reversal method that:
- Performs type checking
- Uses an efficient two-pointer swap technique
- Works with various string types, including Unicode

Args:
input_string (str): The string to be reversed.
Expand All @@ -15,10 +20,10 @@ def reverse_string(input_string):
if not isinstance(input_string, str):
raise TypeError("Input must be a string")

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

# Manually reverse the list of characters
# Manually reverse the list of characters using two-pointer technique
left, right = 0, len(chars) - 1
while left < right:
# Swap characters from both ends
Expand Down
27 changes: 5 additions & 22 deletions src/string_utils.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
def reverse_string(input_string):
"""
Reverse the given string using a manual character-by-character approach.
# This file has been deprecated in favor of string_reversal.py
# The string reversal functionality is now maintained in string_reversal.py

Args:
input_string (str): The string to be reversed.
from .string_reversal import reverse_string

Returns:
str: The reversed string.

Raises:
TypeError: If the input is not a string.
"""
# Type checking
if not isinstance(input_string, str):
raise TypeError("Input must be a string")

# Use list to manually reverse the string
reversed_chars = []
for i in range(len(input_string) - 1, -1, -1):
reversed_chars.append(input_string[i])

# Convert list back to string
return ''.join(reversed_chars)
# Keeping this for backwards compatibility
__all__ = ['reverse_string']
Binary file modified tests/__pycache__/test_string_reversal.cpython-312-pytest-8.3.5.pyc
Binary file not shown.
Binary file modified tests/__pycache__/test_string_utils.cpython-312-pytest-8.3.5.pyc
Binary file not shown.
4 changes: 4 additions & 0 deletions tests/test_string_reversal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def test_reverse_empty_string():
"""Test reversing an empty string."""
assert reverse_string("") == ""

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

def test_reverse_palindrome():
"""Test reversing a palindrome string."""
assert reverse_string("racecar") == "racecar"
Expand Down