Skip to content

Commit 82e0cf3

Browse files
UTSAVS26pre-commit-ci[bot]cclauss
authored
feat(arrays): Add in-place array reversal algorithm (#13370)
* feat(arrays): Add in-place array reversal algorithm * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Enhance docstring for reverse_array function Updated docstring to clarify that the function returns the reversed list. * Clarify return value in reverse_array function Updated docstring to clarify that the function returns the reversed list. Modified example usage to display the reversed array correctly. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 100bf9a commit 82e0cf3

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""
2+
In-place array reversal that also returns the reversed list to the caller.
3+
This algorithm reverses the elements of a list without using extra space.
4+
"""
5+
6+
from typing import Any
7+
8+
9+
def reverse_array(arr: list[Any]) -> list[Any]:
10+
"""
11+
Reverses a list in-place.
12+
13+
This function takes a list and reverses its elements using a two-pointer
14+
approach. The left pointer starts at the beginning of the list, and the
15+
right pointer starts at the end. The elements at these two pointers are
16+
swapped, and the pointers move towards the center until they meet or cross.
17+
18+
Args:
19+
arr: The list to be reversed.
20+
21+
Returns:
22+
The same list, now reversed. This allows for method chaining.
23+
24+
Doctests:
25+
>>> reverse_array([1, 2, 3, 4, 5])
26+
[5, 4, 3, 2, 1]
27+
>>> reverse_array(['a', 'b', 'c', 'd'])
28+
['d', 'c', 'b', 'a']
29+
>>> reverse_array([10.5, 20.2, 30.8])
30+
[30.8, 20.2, 10.5]
31+
>>> reverse_array(["apple", "banana", "cherry"])
32+
['cherry', 'banana', 'apple']
33+
>>> reverse_array([1])
34+
[1]
35+
>>> reverse_array([])
36+
[]
37+
>>> reverse_array(list(range(5)))
38+
[4, 3, 2, 1, 0]
39+
>>> reverse_array(tuple(range(5)))
40+
Traceback (most recent call last):
41+
...
42+
TypeError: 'tuple' object does not support item assignment
43+
>>> reverse_array(range(5))
44+
Traceback (most recent call last):
45+
...
46+
TypeError: 'range' object does not support item assignment
47+
"""
48+
left = 0
49+
right = len(arr) - 1
50+
51+
while left < right:
52+
# Swap the elements at the left and right pointers
53+
arr[left], arr[right] = arr[right], arr[left]
54+
# Move the pointers towards the center
55+
left += 1
56+
right -= 1
57+
return arr
58+
59+
60+
if __name__ == "__main__":
61+
# The doctest module runs the tests embedded in the function's docstring.
62+
# To run the tests, execute this script from the command line:
63+
# python -m doctest -v reverse_array.py
64+
import doctest
65+
66+
doctest.testmod()
67+
68+
# Example usage:
69+
print("\n--- Example Usage ---")
70+
sample_array = [10, 20, 30, 40, 50, 60]
71+
print(f"{sample_array = }")
72+
print(f"{reverse_array(sample_array) = }")

0 commit comments

Comments
 (0)