Skip to content

Implement Robust String Reversal Functionality with Comprehensive Test Coverage #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
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.
26 changes: 26 additions & 0 deletions src/string_reversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def reverse_string(input_string: str) -> str:
"""
Reverse the given input string using a manual character-by-character reversal.

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")

# Create a list to store reversed characters
reversed_chars = []

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

# Join the characters back into a string
return ''.join(reversed_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_standard_string():
"""Test reversing a standard string."""
assert reverse_string("hello") == "olleh"

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

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

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

def test_reverse_with_special_characters():
"""Test reversing a string with special characters."""
assert reverse_string("h3llo!") == "!oll3h"

def test_reverse_with_mixed_characters():
"""Test reversing a string with mixed characters."""
assert reverse_string("A1b2C3!@") == "@!3C2b1A"

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