-
-
Notifications
You must be signed in to change notification settings - Fork 46.5k
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
[NEW ALGORITHM] Rotate linked list by K. #9278
[NEW ALGORITHM] Rotate linked list by K. #9278
Conversation
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.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper
commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper review
to trigger the checks for only added pull request files@algorithms-keeper review-all
to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
return head | ||
|
||
|
||
def right_rotate_by_k(head: Node, k: int) -> Node: |
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.
Please provide descriptive name for the parameter: k
As stated above Also...
|
Co-authored-by: Christian Clauss <cclauss@me.com>
for more information, see https://pre-commit.ci
Co-authored-by: Christian Clauss <cclauss@me.com>
I used the variable "rotation" instead of "k". |
def right_rotate_by_k(head: Node | None, rotation: int) -> Node | None: | ||
""" | ||
Rotate a linked list to the right by rotation times. | ||
|
||
Parameters: | ||
head (Node | None): The head of the linked list. | ||
rotation (int): The number of places to rotate. | ||
|
||
Returns: | ||
Node | None: The head of the rotated linked list\ | ||
if the linked list is not None, otherwise None. |
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.
Mypy can test data types in the function signature, but not in the comments.
Do not repeat the datatypes in both places because readers will be confused if one is changed and the other is not.
How about this naming...
def right_rotate_by_k(head: Node | None, rotation: int) -> Node | None: | |
""" | |
Rotate a linked list to the right by rotation times. | |
Parameters: | |
head (Node | None): The head of the linked list. | |
rotation (int): The number of places to rotate. | |
Returns: | |
Node | None: The head of the rotated linked list\ | |
if the linked list is not None, otherwise None. | |
def rotate_to_the_right(head: Node | None, places: int) -> Node | None: | |
""" | |
Rotate a linked list to the right by a number of places. | |
Parameters: | |
head: The head of the linked list. | |
places: The number of places to rotate the linked list | |
Returns: | |
The head of the rotated linked list or None if there is no list. |
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.
I might even raise a ValueError
if I was handed a head=None.
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.
Do we have a test for a linked list that has just 3 elements but we are asked to shift to the right 10 places?
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.
What happens if we are asked to shift to the right -2 places? -2.5 places?
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.
What happens if we are asked to shift to the right -2 places? -2.5 places?
If the rotation number is not an integer, it should raise an error. If it is a negative integer, converting it to a positive integer would be a better idea, right?
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.
Do we have a test for a linked list that has just 3 elements but we are asked to shift to the right 10 places?
Yes, I tested it, and it is working correctly.
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.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper
commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper review
to trigger the checks for only added pull request files@algorithms-keeper review-all
to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
* Rotate linked list by k. * Rotate linked list by k. * updated variable name. * Update data_structures/linked_list/rotate_linked_list_by_k.py Co-authored-by: Christian Clauss <cclauss@me.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update data_structures/linked_list/rotate_linked_list_by_k.py Co-authored-by: Christian Clauss <cclauss@me.com> * Update data_structures/linked_list/rotate_linked_list_by_k.py * Make Node a dataclass --------- Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Describe your change:
I have added a new algorithm to rotate a linked list by k times. I have also:
Added the function docstring to provide clear documentation.
Added type hints for function parameters and return values.
Included comments to enhance code readability.
Checklist: