From 6e1467d0ab59bf6fee8ec0c46e63a8ff8695bf58 Mon Sep 17 00:00:00 2001 From: arvinder004 Date: Thu, 2 Oct 2025 13:39:22 +0530 Subject: [PATCH 1/3] added rotate array function --- data_structures/arrays/rotate_array.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 data_structures/arrays/rotate_array.py diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py new file mode 100644 index 000000000000..1d8615285c54 --- /dev/null +++ b/data_structures/arrays/rotate_array.py @@ -0,0 +1,25 @@ +def rotate_array(arr, k): + """ + Rotates the array to the right by k steps + + Args: + arr(list): The input array + k(int): Number of steps to rotate + + Returns: + list: Rotated array + """ + n = len(arr) + k = k % n if n else 0 + + arr.reverse() # Reverse the entire array + arr[:k] = reversed(arr[:k]) # Reverse the first k elements + arr[k:] = reversed(arr[k:]) # Reverse the last (n-k) elements + + return arr + + +if __name__ == "__main__": + arr = [1, 2, 3, 4, 5, 6, 7, 8] + k = 3 + print("Rotated array: ", rotate_array(arr, k)) From 43b6c5ea337dd01bcbf9117c9ddf3094dc0aca87 Mon Sep 17 00:00:00 2001 From: arvinder004 Date: Thu, 2 Oct 2025 13:44:17 +0530 Subject: [PATCH 2/3] reviewed --- data_structures/arrays/rotate_array.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py index 1d8615285c54..f9679a79472f 100644 --- a/data_structures/arrays/rotate_array.py +++ b/data_structures/arrays/rotate_array.py @@ -1,13 +1,21 @@ -def rotate_array(arr, k): +def rotate_array(arr: list[int], k: int) -> list[int]: """ Rotates the array to the right by k steps Args: - arr(list): The input array - k(int): Number of steps to rotate + arr (list[int]): The input array. + k (int): Number of steps to rotate. Returns: - list: Rotated array + list[int]: Rotated array. + + Examples: + >>> rotate_array([1, 2, 3, 4, 5], 2) + [4, 5, 1, 2, 3] + >>> rotate_array([1, 2, 3], 0) + [1, 2, 3] + >>> rotate_array([], 3) + [] """ n = len(arr) k = k % n if n else 0 @@ -21,5 +29,5 @@ def rotate_array(arr, k): if __name__ == "__main__": arr = [1, 2, 3, 4, 5, 6, 7, 8] - k = 3 - print("Rotated array: ", rotate_array(arr, k)) + steps = 3 + print("Rotated array: ", rotate_array(arr, steps)) From 02b6dcac571797ed085370faacb0345f8a35e4cf Mon Sep 17 00:00:00 2001 From: arvinder004 Date: Thu, 2 Oct 2025 13:45:41 +0530 Subject: [PATCH 3/3] reviewed --- data_structures/arrays/rotate_array.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py index f9679a79472f..bb09564dfaf0 100644 --- a/data_structures/arrays/rotate_array.py +++ b/data_structures/arrays/rotate_array.py @@ -1,6 +1,6 @@ -def rotate_array(arr: list[int], k: int) -> list[int]: +def rotate_array(arr: list[int], steps: int) -> list[int]: """ - Rotates the array to the right by k steps + Rotates the array to the right by a given number of steps Args: arr (list[int]): The input array. @@ -18,7 +18,7 @@ def rotate_array(arr: list[int], k: int) -> list[int]: [] """ n = len(arr) - k = k % n if n else 0 + k = steps % n if n else 0 arr.reverse() # Reverse the entire array arr[:k] = reversed(arr[:k]) # Reverse the first k elements