Skip to content

Commit 90b332b

Browse files
committed
changed k to steps for a descriptive name
1 parent 7043856 commit 90b332b

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

data_structures/arrays/rotate_array.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from typing import List
22

33

4-
def rotate_array(arr: List[int], k: int) -> List[int]:
4+
def rotate_array(arr: List[int], steps: int) -> List[int]:
55
"""
6-
Rotates a list to the right by k positions.
6+
Rotates a list to the right by steps positions.
77
88
Parameters:
99
arr (List[int]): The list of integers to rotate.
10-
k (int): Number of positions to rotate. Can be negative for left rotation.
10+
steps (int): Number of positions to rotate. Can be negative for left rotation.
1111
1212
Returns:
1313
List[int]: Rotated list.
@@ -27,10 +27,10 @@ def rotate_array(arr: List[int], k: int) -> List[int]:
2727
if n == 0:
2828
return arr
2929

30-
k = k % n
30+
steps = steps % n
3131

32-
if k < 0:
33-
k += n
32+
if steps < 0:
33+
steps += n
3434

3535
def reverse(start: int, end: int) -> None:
3636
"""
@@ -57,15 +57,15 @@ def reverse(start: int, end: int) -> None:
5757
>>> example
5858
[3, 2, 5, 4, 1]
5959
"""
60-
60+
6161
while start < end:
6262
arr[start], arr[end] = arr[end], arr[start]
6363
start += 1
6464
end -= 1
6565

6666
reverse(0, n - 1)
67-
reverse(0, k - 1)
68-
reverse(k, n - 1)
67+
reverse(0, steps - 1)
68+
reverse(steps, n - 1)
6969

7070
return arr
7171

@@ -78,6 +78,6 @@ def reverse(start: int, end: int) -> None:
7878
([], 3),
7979
]
8080

81-
for arr, k in examples:
82-
rotated = rotate_array(arr.copy(), k)
83-
print(f"Rotate {arr} by {k}: {rotated}")
81+
for arr, steps in examples:
82+
rotated = rotate_array(arr.copy(), steps)
83+
print(f"Rotate {arr} by {steps}: {rotated}")

0 commit comments

Comments
 (0)