Skip to content
Merged
Changes from 1 commit
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
34 changes: 34 additions & 0 deletions data_structures/arrays/rotate_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
def rotate_array(arr, k):

Choose a reason for hiding this comment

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

Please provide return type hint for the function: rotate_array. If the function does not return a value, please provide the type hint as: def function() -> None:

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 rotate_array

Please provide type hint for the parameter: arr

Please provide type hint for the parameter: k

Please provide descriptive name for the parameter: 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}")

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

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W292)

data_structures/arrays/rotate_array.py:34:49: W292 No newline at end of file
Loading