Skip to content

Commit

Permalink
Update reverse_string.py
Browse files Browse the repository at this point in the history
  • Loading branch information
FKN237 authored Jan 12, 2025
1 parent 09ee56e commit a48971f
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion solutions/reverse_string.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
def reverse_string(s):
"""
This module provides a function to reverse an inputed string.
Function:
reverse_string(s: str) -> str:
Reverses the input string and returns the reversed one.
Features:
- Simple and efficient string reversal.
- Built-in error handling for invalid inputs.
Raises:
TypeError: If the input `s` is not a string.
ValueError: If the input `s` is an empty string.
Example:
>>> reverse_string("Deadline")
enildaeD
"""


def reverse_string(s: str) -> str:
"""
Reverses the given string.
Expand All @@ -11,5 +31,13 @@ def reverse_string(s):
Example:
>>> reverse_string("hello")
'olleh'
>>> reverse_string("12345")
'54321'
"""
# Defensive assertion: Ensure the input is a string
if not isinstance(s, str):
raise TypeError("Input must be a string")
# Defensive assertion: Ensure the string is not empty
if not s:
raise ValueError("Input string cannot be empty")
return s[::-1] # Return the reversed string using slicing

0 comments on commit a48971f

Please sign in to comment.