Skip to content

Updated singly_linked_list - #2477

Merged
cclauss merged 15 commits into
TheAlgorithms:masterfrom
realDuYuanChao:singly_linked_list
Sep 25, 2020
Merged

Updated singly_linked_list#2477
cclauss merged 15 commits into
TheAlgorithms:masterfrom
realDuYuanChao:singly_linked_list

Conversation

@realDuYuanChao

@realDuYuanChaorealDuYuanChao commented Sep 25, 2020

Copy link
Copy Markdown
Member

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}.

Comment threaddata_structures/linked_list/singly_linked_list.py Outdated

@realDuYuanChaorealDuYuanChao left a comment

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

@cclauss Done.

Comment threaddata_structures/linked_list/singly_linked_list.py Outdated
class LinkedList:
def __init__(self):
self.head = None # initialize head to None
self.size = 0 # length of linked list

@cclausscclaussSep 25, 2020

Copy link
Copy Markdown
Member

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.

@realDuYuanChaorealDuYuanChaoSep 25, 2020

Copy link
Copy Markdown
MemberAuthor

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.

@cclausscclaussSep 25, 2020

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
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.headwhilenode:
yieldnode.datanode=node.next

Copy link
Copy Markdown
MemberAuthor

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
Copy Markdown
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.

@realDuYuanChaorealDuYuanChaoSep 25, 2020

Copy link
Copy Markdown
MemberAuthor

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
Copy Markdown

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
Copy Markdown

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
Copy Markdown
MemberAuthor

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

Comment threaddata_structures/linked_list/singly_linked_list.py Outdated
Comment threaddata_structures/linked_list/singly_linked_list.py Outdated

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
MemberAuthor

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 ?

Comment threaddata_structures/linked_list/singly_linked_list.py
Comment threaddata_structures/linked_list/singly_linked_list.py Outdated
Comment threaddata_structures/linked_list/singly_linked_list.py Outdated
Comment threaddata_structures/linked_list/singly_linked_list.py Outdated
Comment threaddata_structures/linked_list/singly_linked_list.py Outdated
Comment threaddata_structures/linked_list/singly_linked_list.py Outdated
realDuYuanChaoand 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>

@cclausscclauss left a comment

Copy link
Copy Markdown
Member

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:masterSep 25, 2020
@cclausscclauss 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 freeto 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

@realDuYuanChao@TravisBuddy@cclauss