-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Updated singly_linked_list #2477
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
Updated singly_linked_list #2477
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.
@cclauss Done.
@@ -10,46 +10,54 @@ def __repr__(self): | |||
class LinkedList: | |||
def __init__(self): | |||
self.head = None # initialize head to None | |||
self.size = 0 # length of linked 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 am not a fan of this change. This creates duplicate state and opens us up to too many locations where this variable must be accurately updated or we have bugs that are difficult to debug.
Instead, please consider creating an .__iter__()
method. Then .__len__()
becomes return len(tuple(self))
. This could also simplify other methods like .__repr__()
, .__str__()
, etc.
There are examples of creating this method elsewhere in this repo.
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.
Can you please give me an example file link in this repo.
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.
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.
def __iter__(self):
node = self.head
while node:
yield node.data
node = node.next
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.
add this add source file ?
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.
Maybe it is just me but I like to sort the .__xxx__()
methods above the other methods so the reader knows the builtin capabilities before reading the custom methods.
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.
Could you please clone this this branch and commit what you want. I am not similar to implement __iter__
. Thanks in advance.
This will break tests, etc.
Travis tests have failedHey @shellhub, TravisBuddy Request Identifier: 9f1dd860-ff19-11ea-b444-3dcc0bd866ab |
Travis tests have failedHey @shellhub, TravisBuddy Request Identifier: 56144e80-ff1c-11ea-b444-3dcc0bd866ab |
@cclauss It seems build successful. But one Travic CI in Queued. Not running. |
|
||
def __getitem__(self, index): | ||
""" | ||
Indexing Support. Used to get a node at particular position |
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.
doctests needed?
|
||
# Used to change the data of a particular node | ||
def __setitem__(self, index, data): | ||
current = self.head |
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.
doctests needed?
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 you mean we need add doctests here ?
Co-authored-by: Christian Clauss <[email protected]>
Co-authored-by: Christian Clauss <[email protected]>
Co-authored-by: Christian Clauss <[email protected]>
Co-authored-by: Christian Clauss <[email protected]>
Co-authored-by: Christian Clauss <[email protected]>
Co-authored-by: Christian Clauss <[email protected]>
Co-authored-by: Christian Clauss <[email protected]>
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.
LGTM
* Updated singly_linked_list * fixup! Format Python code with psf/black push * undo __repr__ * updating DIRECTORY.md * UNTESTED CHANGES: Add an .__iter__() method. This will break tests, etc. * fixup! Format Python code with psf/black push * len(tuple(iter(self))) * fixed __repr__() * Update data_structures/linked_list/singly_linked_list.py Co-authored-by: Christian Clauss <[email protected]> * Update data_structures/linked_list/singly_linked_list.py Co-authored-by: Christian Clauss <[email protected]> * Update data_structures/linked_list/singly_linked_list.py Co-authored-by: Christian Clauss <[email protected]> * Update data_structures/linked_list/singly_linked_list.py Co-authored-by: Christian Clauss <[email protected]> * Update data_structures/linked_list/singly_linked_list.py Co-authored-by: Christian Clauss <[email protected]> * Update data_structures/linked_list/singly_linked_list.py Co-authored-by: Christian Clauss <[email protected]> * Update data_structures/linked_list/singly_linked_list.py Co-authored-by: Christian Clauss <[email protected]> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
Describe your change:
Checklist:
Fixes: #{$ISSUE_NO}
.