Skip to content
Merged
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions data_structures/arrays/rotate_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from typing import List

Check failure on line 1 in data_structures/arrays/rotate_array.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP035)

data_structures/arrays/rotate_array.py:1:1: UP035 `typing.List` is deprecated, use `list` instead


def rotate_array(arr: List[int], k: int) -> List[int]:

Check failure on line 4 in data_structures/arrays/rotate_array.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/arrays/rotate_array.py:4:45: UP006 Use `list` instead of `List` for type annotation

Check failure on line 4 in data_structures/arrays/rotate_array.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP006)

data_structures/arrays/rotate_array.py:4:23: UP006 Use `list` instead of `List` for type annotation

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: k

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single-letter variable names are uncool, as discussed in CONTRIBUTING.md.

"""
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

k = k % n

if k < 0:
k += n

def reverse(start: int, end: int) -> None:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/rotate_array.py, please provide doctest for the function reverse

"""
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]
"""

Check failure on line 60 in data_structures/arrays/rotate_array.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

data_structures/arrays/rotate_array.py:60:1: W293 Blank line contains whitespace
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}")
Loading