Skip to content

Commit 59fd79b

Browse files
author
Milad Khoshdel
committed
Add docstrings for string reversal methods with complexity details
1 parent f6be977 commit 59fd79b

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

algorithms/strings/reverse_string.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,71 @@
1+
"""
2+
This module demonstrates multiple methods for reversing a string in Python.
3+
4+
Summary of String Reversal Implementations
5+
6+
| Function | Time Complexity | Space Complexity | Notes |
7+
|------------------|------------------|------------------|------------------------------------|
8+
| recursive | O(n log n) | O(n) | Recursive with slicing and concat |
9+
| iterative | O(n) | O(n) | In-place list swapping |
10+
| pythonic | O(n) | O(n) | Uses reversed() and join() |
11+
| ultra_pythonic | O(n) | O(n) | Uses slicing (s[::-1]) |
12+
13+
All functions return the reversed version of the input string.
14+
Note: Strings in Python are immutable, so all methods produce a new string.
15+
"""
16+
117
def recursive(s):
18+
"""
19+
Reverses a given string using a recursive divide-and-conquer approach.
20+
21+
This function recursively splits the input string `s` into two halves,
22+
reverses each half by further recursive calls, and then concatenates
23+
them in reverse order to form the final reversed string.
24+
25+
Args:
26+
s (str): The string to be reversed.
27+
28+
Returns:
29+
str: The reversed string.
30+
31+
Example:
32+
>>> recursive("hello")
33+
'olleh'
34+
35+
Note:
36+
- This approach is primarily educational.
37+
- Time complexity is O(n log n) due to repeated slicing and concatenation.
38+
- Space complexity is also higher because of the call stack and substring creation.
39+
- It may hit Python's recursion depth limit on very long strings.
40+
"""
241
l = len(s)
342
if l < 2:
443
return s
544
return recursive(s[l//2:]) + recursive(s[:l//2])
645

746
def iterative(s):
47+
"""
48+
Reverses a given string using an iterative approach with two-pointer swapping.
49+
50+
This function converts the string `s` into a list of characters and then iteratively swaps
51+
the characters from the start and end of the list, moving towards the center. The process
52+
continues until the entire string is reversed.
53+
54+
Args:
55+
s (str): The string to be reversed.
56+
57+
Returns:
58+
str: The reversed string.
59+
60+
Example:
61+
>>> iterative("hello")
62+
'olleh'
63+
64+
Note:
65+
- Time complexity is O(n), where n is the length of the string `s`.
66+
- Space complexity is O(n) due to the list copy of the string.
67+
- The actual reversal is performed in-place on the list using O(1) additional working space.
68+
"""
869
r = list(s)
970
i, j = 0, len(s) - 1
1071
while i < j:
@@ -14,7 +75,48 @@ def iterative(s):
1475
return "".join(r)
1576

1677
def pythonic(s):
78+
"""
79+
Reverses a given string using the built-in `reversed()` function.
80+
81+
This function returns a new string that is the reverse of the input string `s`.
82+
It uses the `reversed()` function to create an iterator over the input string,
83+
and then joins the elements of the iterator to form the reversed string.
84+
85+
Args:
86+
s (str): The string to be reversed.
87+
88+
Returns:
89+
str: The reversed string.
90+
91+
Example:
92+
>>> pythonic("hello")
93+
'olleh'
94+
95+
Note:
96+
This function has a time complexity of O(n), where n is the length of the string `s`.
97+
It does not modify the original string, as strings in Python are immutable.
98+
"""
1799
return "".join(reversed(s))
18100

19101
def ultra_pythonic(s):
102+
"""
103+
Reverses a given string using Python's slicing feature.
104+
105+
This function utilizes Python's slicing syntax to reverse the input string `s`.
106+
It returns a new string where the characters of `s` are arranged in reverse order.
107+
108+
Args:
109+
s (str): The string to be reversed.
110+
111+
Returns:
112+
str: The reversed string.
113+
114+
Example:
115+
>>> ultra_pythonic("hello")
116+
'olleh'
117+
118+
Note:
119+
This function is concise and efficient, with a time complexity of O(n),
120+
where n is the length of the string `s`. It does not modify the original string.
121+
"""
20122
return s[::-1]

0 commit comments

Comments
 (0)