-
-
Notifications
You must be signed in to change notification settings - Fork 48.6k
added rotate_array.py #13336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
added rotate_array.py #13336
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
02f2a78
added rotate_array.py
jenyyy4 ded9c6d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7096968
fixed issues
jenyyy4 302988d
merged branch
jenyyy4 bd4e433
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 925cb2d
fixed reverse issue
jenyyy4 85eb704
Merge branch 'anuska' of https://github.com/jenyyy4/Python into anuska
jenyyy4 7043856
added doctests
jenyyy4 889aa02
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 90b332b
changed k to steps for a descriptive name
jenyyy4 8aa6469
Merge branch 'anuska' of https://github.com/jenyyy4/Python into anuska
jenyyy4 ec22c92
Merge branch 'master' into anuska
jenyyy4 5a1cf10
fixed non-pep
jenyyy4 79dc1ad
Merge branch 'anuska' of https://github.com/jenyyy4/Python into anuska
jenyyy4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
def rotate_array(arr: list[int], steps: int) -> list[int]: | ||
""" | ||
Rotates a list to the right by steps positions. | ||
Parameters: | ||
arr (List[int]): The list of integers to rotate. | ||
steps (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 | ||
|
||
steps = steps % n | ||
|
||
if steps < 0: | ||
steps += 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 | ||
end -= 1 | ||
|
||
reverse(0, n - 1) | ||
reverse(0, steps - 1) | ||
reverse(steps, 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, steps in examples: | ||
rotated = rotate_array(arr.copy(), steps) | ||
print(f"Rotate {arr} by {steps}: {rotated}") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 functionreverse