Skip to content

Commit 7043856

Browse files
committed
added doctests
1 parent 85eb704 commit 7043856

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

data_structures/arrays/rotate_array.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22

33

44
def rotate_array(arr: List[int], k: int) -> List[int]:
5+
"""
6+
Rotates a list to the right by k positions.
7+
8+
Parameters:
9+
arr (List[int]): The list of integers to rotate.
10+
k (int): Number of positions to rotate. Can be negative for left rotation.
11+
12+
Returns:
13+
List[int]: Rotated list.
14+
15+
Examples:
16+
>>> rotate_array([1, 2, 3, 4, 5], 2)
17+
[4, 5, 1, 2, 3]
18+
>>> rotate_array([1, 2, 3, 4, 5], -2)
19+
[3, 4, 5, 1, 2]
20+
>>> rotate_array([1, 2, 3, 4, 5], 7)
21+
[4, 5, 1, 2, 3]
22+
>>> rotate_array([], 3)
23+
[]
24+
"""
25+
526
n = len(arr)
627
if n == 0:
728
return arr
@@ -12,6 +33,31 @@ def rotate_array(arr: List[int], k: int) -> List[int]:
1233
k += n
1334

1435
def reverse(start: int, end: int) -> None:
36+
"""
37+
Reverses a portion of the list in place from index start to end.
38+
39+
Parameters:
40+
start (int): Starting index of the portion to reverse.
41+
end (int): Ending index of the portion to reverse.
42+
43+
Returns:
44+
None
45+
46+
Examples:
47+
>>> example = [1, 2, 3, 4, 5]
48+
>>> def reverse_test(arr, start, end):
49+
... while start < end:
50+
... arr[start], arr[end] = arr[end], arr[start]
51+
... start += 1
52+
... end -= 1
53+
>>> reverse_test(example, 0, 2)
54+
>>> example
55+
[3, 2, 1, 4, 5]
56+
>>> reverse_test(example, 2, 4)
57+
>>> example
58+
[3, 2, 5, 4, 1]
59+
"""
60+
1561
while start < end:
1662
arr[start], arr[end] = arr[end], arr[start]
1763
start += 1

0 commit comments

Comments
 (0)