Skip to content
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

Merged
merged 15 commits into from
Sep 25, 2020
Merged

Updated singly_linked_list #2477

merged 15 commits into from
Sep 25, 2020

Conversation

realDuYuanChao
Copy link
Member

@realDuYuanChao realDuYuanChao commented Sep 25, 2020

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Copy link
Member Author

@realDuYuanChao realDuYuanChao left a comment

Choose a reason for hiding this comment

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

@cclauss Done.

data_structures/linked_list/singly_linked_list.py Outdated Show resolved Hide resolved
@@ -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
Copy link
Member

@cclauss cclauss Sep 25, 2020

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.

Copy link
Member Author

@realDuYuanChao realDuYuanChao Sep 25, 2020

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.

Copy link
Member

@cclauss cclauss Sep 25, 2020

Choose a reason for hiding this comment

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

Copy link
Member

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

Copy link
Member Author

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 ?

Copy link
Member

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.

Copy link
Member Author

@realDuYuanChao realDuYuanChao Sep 25, 2020

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.

@TravisBuddy
Copy link

Travis tests have failed

Hey @shellhub,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: 9f1dd860-ff19-11ea-b444-3dcc0bd866ab

@TravisBuddy
Copy link

Travis tests have failed

Hey @shellhub,
Please read the following log in order to understand the failure reason.
It'll be awesome if you fix what's wrong and commit the changes.

TravisBuddy Request Identifier: 56144e80-ff1c-11ea-b444-3dcc0bd866ab

@realDuYuanChao
Copy link
Member Author

@cclauss It seems build successful. But one Travic CI in Queued. Not running.

data_structures/linked_list/singly_linked_list.py Outdated Show resolved Hide resolved
data_structures/linked_list/singly_linked_list.py Outdated Show resolved Hide resolved

def __getitem__(self, index):
"""
Indexing Support. Used to get a node at particular position
Copy link
Member

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
Copy link
Member

Choose a reason for hiding this comment

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

doctests needed?

Copy link
Member Author

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 ?

data_structures/linked_list/singly_linked_list.py Outdated Show resolved Hide resolved
data_structures/linked_list/singly_linked_list.py Outdated Show resolved Hide resolved
data_structures/linked_list/singly_linked_list.py Outdated Show resolved Hide resolved
data_structures/linked_list/singly_linked_list.py Outdated Show resolved Hide resolved
data_structures/linked_list/singly_linked_list.py Outdated Show resolved Hide resolved
data_structures/linked_list/singly_linked_list.py Outdated Show resolved Hide resolved
realDuYuanChao and others added 7 commits September 25, 2020 21:09
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Copy link
Member

@cclauss cclauss left a comment

Choose a reason for hiding this comment

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

LGTM

@cclauss cclauss merged commit 18f1dcd into TheAlgorithms:master Sep 25, 2020
@cclauss cclauss mentioned this pull request Sep 26, 2020
10 tasks
stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
* 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 <cclauss@me.com>

* Update data_structures/linked_list/singly_linked_list.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update data_structures/linked_list/singly_linked_list.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update data_structures/linked_list/singly_linked_list.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update data_structures/linked_list/singly_linked_list.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update data_structures/linked_list/singly_linked_list.py

Co-authored-by: Christian Clauss <cclauss@me.com>

* Update data_structures/linked_list/singly_linked_list.py

Co-authored-by: Christian Clauss <cclauss@me.com>

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants