From 02f2a78d852e8095f3885a875c78874fc2353aff Mon Sep 17 00:00:00 2001 From: jenyyy4 Date: Wed, 8 Oct 2025 03:00:53 +0530 Subject: [PATCH 1/9] added rotate_array.py --- data_structures/arrays/rotate_array.py | 34 ++++++++++++++++++++++++++ 1 file changed, 34 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..7804f2683879 --- /dev/null +++ b/data_structures/arrays/rotate_array.py @@ -0,0 +1,34 @@ +def rotate_array(arr, k): + n = len(arr) + if n == 0: + return arr + + k = k % n + + if k < 0: + k += n + + def reverse(start, end): + while start < end: + arr[start], arr[end] = arr[end], arr[start] + start += 1 + end -= 1 + + reverse(0, n - 1) + reverse(0, k - 1) + reverse(k, n - 1) + + return arr + + +if __name__ == "__main__": + examples = [ + ([1, 2, 3, 4, 5], 2), + ([1, 2, 3, 4, 5], -2), + ([1, 2, 3, 4, 5], 7), + ([], 3), + ] + + for arr, k in examples: + rotated = rotate_array(arr.copy(), k) + print(f"Rotate {arr} by {k}: {rotated}") \ No newline at end of file From ded9c6d286551907bc03bfb11c5a85e5f2c9adab Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 7 Oct 2025 21:35:37 +0000 Subject: [PATCH 2/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/rotate_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py index 7804f2683879..c7a7ab36090c 100644 --- a/data_structures/arrays/rotate_array.py +++ b/data_structures/arrays/rotate_array.py @@ -31,4 +31,4 @@ def reverse(start, end): for arr, k in examples: rotated = rotate_array(arr.copy(), k) - print(f"Rotate {arr} by {k}: {rotated}") \ No newline at end of file + print(f"Rotate {arr} by {k}: {rotated}") From 7096968ba3c420f3fc4d7d0af9fdce7e834792d8 Mon Sep 17 00:00:00 2001 From: jenyyy4 Date: Wed, 8 Oct 2025 03:08:24 +0530 Subject: [PATCH 3/9] fixed issues --- data_structures/arrays/rotate_array.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py index 7804f2683879..9e1678ad73f1 100644 --- a/data_structures/arrays/rotate_array.py +++ b/data_structures/arrays/rotate_array.py @@ -1,4 +1,6 @@ -def rotate_array(arr, k): +from typing import List + +def rotate_array(arr: List[int], k: int) -> List[int]: n = len(arr) if n == 0: return arr From bd4e433a95b6203acbbf0133688456b945ca4f4e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 7 Oct 2025 21:41:41 +0000 Subject: [PATCH 4/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/rotate_array.py | 1 + 1 file changed, 1 insertion(+) diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py index b30a7e60c2f0..f3a3f21b74dd 100644 --- a/data_structures/arrays/rotate_array.py +++ b/data_structures/arrays/rotate_array.py @@ -1,5 +1,6 @@ from typing import List + def rotate_array(arr: List[int], k: int) -> List[int]: n = len(arr) if n == 0: From 925cb2d3efe79e82a323b9202db0395159a7a022 Mon Sep 17 00:00:00 2001 From: jenyyy4 Date: Wed, 8 Oct 2025 03:12:14 +0530 Subject: [PATCH 5/9] fixed reverse issue --- data_structures/arrays/rotate_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py index b30a7e60c2f0..d392d750b7d1 100644 --- a/data_structures/arrays/rotate_array.py +++ b/data_structures/arrays/rotate_array.py @@ -10,7 +10,7 @@ def rotate_array(arr: List[int], k: int) -> List[int]: if k < 0: k += n - def reverse(start, end): + def reverse(start: int, end: int) -> None: while start < end: arr[start], arr[end] = arr[end], arr[start] start += 1 From 7043856d446f2ed77ad41ca028f6483e2ba00b27 Mon Sep 17 00:00:00 2001 From: jenyyy4 Date: Wed, 8 Oct 2025 03:14:10 +0530 Subject: [PATCH 6/9] added doctests --- data_structures/arrays/rotate_array.py | 46 ++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py index ae54441f3c3f..0e791d88061b 100644 --- a/data_structures/arrays/rotate_array.py +++ b/data_structures/arrays/rotate_array.py @@ -2,6 +2,27 @@ def rotate_array(arr: List[int], k: int) -> List[int]: + """ + Rotates a list to the right by k positions. + + Parameters: + arr (List[int]): The list of integers to rotate. + k (int): Number of positions to rotate. Can be negative for left rotation. + + Returns: + List[int]: Rotated list. + + Examples: + >>> rotate_array([1, 2, 3, 4, 5], 2) + [4, 5, 1, 2, 3] + >>> rotate_array([1, 2, 3, 4, 5], -2) + [3, 4, 5, 1, 2] + >>> rotate_array([1, 2, 3, 4, 5], 7) + [4, 5, 1, 2, 3] + >>> rotate_array([], 3) + [] + """ + n = len(arr) if n == 0: return arr @@ -12,6 +33,31 @@ def rotate_array(arr: List[int], k: int) -> List[int]: k += n def reverse(start: int, end: int) -> None: + """ + Reverses a portion of the list in place from index start to end. + + Parameters: + start (int): Starting index of the portion to reverse. + end (int): Ending index of the portion to reverse. + + Returns: + None + + Examples: + >>> example = [1, 2, 3, 4, 5] + >>> def reverse_test(arr, start, end): + ... while start < end: + ... arr[start], arr[end] = arr[end], arr[start] + ... start += 1 + ... end -= 1 + >>> reverse_test(example, 0, 2) + >>> example + [3, 2, 1, 4, 5] + >>> reverse_test(example, 2, 4) + >>> example + [3, 2, 5, 4, 1] + """ + while start < end: arr[start], arr[end] = arr[end], arr[start] start += 1 From 889aa0235dffa938429412a6b5120669387c2001 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 7 Oct 2025 21:44:34 +0000 Subject: [PATCH 7/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/rotate_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py index 0e791d88061b..6e0688b1a059 100644 --- a/data_structures/arrays/rotate_array.py +++ b/data_structures/arrays/rotate_array.py @@ -57,7 +57,7 @@ def reverse(start: int, end: int) -> None: >>> example [3, 2, 5, 4, 1] """ - + while start < end: arr[start], arr[end] = arr[end], arr[start] start += 1 From 90b332bdb2111a6446ac95f69174e3cf4bff632e Mon Sep 17 00:00:00 2001 From: jenyyy4 Date: Wed, 8 Oct 2025 03:15:05 +0530 Subject: [PATCH 8/9] changed k to steps for a descriptive name --- data_structures/arrays/rotate_array.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py index 0e791d88061b..212dc38d0aa7 100644 --- a/data_structures/arrays/rotate_array.py +++ b/data_structures/arrays/rotate_array.py @@ -1,13 +1,13 @@ from typing import List -def rotate_array(arr: List[int], k: int) -> List[int]: +def rotate_array(arr: List[int], steps: int) -> List[int]: """ - Rotates a list to the right by k positions. + Rotates a list to the right by steps positions. Parameters: arr (List[int]): The list of integers to rotate. - k (int): Number of positions to rotate. Can be negative for left rotation. + steps (int): Number of positions to rotate. Can be negative for left rotation. Returns: List[int]: Rotated list. @@ -27,10 +27,10 @@ def rotate_array(arr: List[int], k: int) -> List[int]: if n == 0: return arr - k = k % n + steps = steps % n - if k < 0: - k += n + if steps < 0: + steps += n def reverse(start: int, end: int) -> None: """ @@ -57,15 +57,15 @@ def reverse(start: int, end: int) -> None: >>> example [3, 2, 5, 4, 1] """ - + while start < end: arr[start], arr[end] = arr[end], arr[start] start += 1 end -= 1 reverse(0, n - 1) - reverse(0, k - 1) - reverse(k, n - 1) + reverse(0, steps - 1) + reverse(steps, n - 1) return arr @@ -78,6 +78,6 @@ def reverse(start: int, end: int) -> None: ([], 3), ] - for arr, k in examples: - rotated = rotate_array(arr.copy(), k) - print(f"Rotate {arr} by {k}: {rotated}") + for arr, steps in examples: + rotated = rotate_array(arr.copy(), steps) + print(f"Rotate {arr} by {steps}: {rotated}") From 5a1cf10e4bdd72cff8ac29e4339e382545a63046 Mon Sep 17 00:00:00 2001 From: jenyyy4 Date: Wed, 8 Oct 2025 16:24:48 +0530 Subject: [PATCH 9/9] fixed non-pep --- data_structures/arrays/rotate_array.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/data_structures/arrays/rotate_array.py b/data_structures/arrays/rotate_array.py index 212dc38d0aa7..d5ce4b4078b3 100644 --- a/data_structures/arrays/rotate_array.py +++ b/data_structures/arrays/rotate_array.py @@ -1,7 +1,4 @@ -from typing import List - - -def rotate_array(arr: List[int], steps: int) -> List[int]: +def rotate_array(arr: list[int], steps: int) -> list[int]: """ Rotates a list to the right by steps positions.