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.
12 changes: 5 additions & 7 deletions src/string_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def reverse_string(input_string):
def reverse_string(input_string: str) -> str:
"""
Reverse the given string using a manual character-by-character approach.

Expand All @@ -11,14 +11,12 @@ def reverse_string(input_string):
Raises:
TypeError: If the input is not a string.
"""
# Type checking
# Type checking with additional type hints
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])
# Use efficient list comprehension for reversal
reversed_chars = [input_string[i] for i in range(len(input_string) - 1, -1, -1)]

# Convert list back to string
# Convert list back to string and return
return ''.join(reversed_chars)
Binary file not shown.
5 changes: 5 additions & 0 deletions tests/test_string_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ 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 strings with spaces."""
assert reverse_string("hello world") == "dlrow olleh"

def test_reverse_string_special_chars():
"""Test reversal of strings with special characters."""
assert reverse_string("a!b@c#") == "#c@b!a"
assert reverse_string("a1b2c3!@#") == "#@!3c2b1a"

def test_reverse_string_unicode():
"""Test reversal of unicode strings."""
Expand Down