Skip to content

Hacktoberfest: Update Linked List - print_reverse method #2792

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

Conversation

shermanhui
Copy link
Contributor

@shermanhui shermanhui commented Oct 5, 2020

Describe your change:

Update make_linked_list helper to use a generator expression instead of
slicing the original elements_list to avoid creating a temporary list
to provide an example that works in O(1) space and O(n) time complexity.
This change is on L46.

I've also added doctests to this file to add test coverage for
expected behaviour and edge cases. When adding the tests, I also
added an additional type check in print_reverse L58 as I discovered
the function would break when providing an argument that is not a
Node instance.

I've checked off "Fix a bug or typo in an existing algorithm" as it
seems to be the option that closest reflects what I've done in this PR.

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

Use a generator expression instead of slicing
`elements_list` to improve the space and time complexity
of `make_linked_list` to O(1) space and O(n) time
by avoiding the creation a shallow copy of `elements_list`.
Add argument typing to all methods in `print_reverse`

Add doctest to helper function `make_linked_list` and
basic edge case tests to `print_reverse`
Fix doctest syntax and remove edge case tests that are covered
by typed arguments.

Add `print_reverse` test that expects the correct values are printed
out by adding a `test_print_reverse_output` helper function.
@shermanhui shermanhui force-pushed the update-print-reverse-linked-list branch from c695884 to 4b1e4f8 Compare October 5, 2020 04:51
@realDuYuanChao
Copy link
Member

@shermanhui I formatted the code. Hope you do not mind

@realDuYuanChao realDuYuanChao merged commit 477b2c2 into TheAlgorithms:master Oct 5, 2020
for data in elements_list[1:]:
current.next = Node(data)
current = head = Node(elements_list[0])
for i in range(1, len(elements_list)):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shellhub Thanks for the review and merge, I was hoping to improve the algorithm by using the generator as a part of my Hacktoberfest contribution for #2510. What was the reason for removing it for the regular iteration over a list?

i.e. (what I had before)

for data in (elements_list[i] for i in range(1, list_length)):

Copy link
Member

Choose a reason for hiding this comment

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

This way need to generate a list and then iterate. It's slow.

Copy link
Member

@realDuYuanChao realDuYuanChao Oct 5, 2020

Choose a reason for hiding this comment

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

def fun1() -> None:
    my_list = []
    elements_list = [14, 52, 14, 12, 43]
    for i in range(0, len(elements_list)):
        my_list.append(i)


def fun2() -> None:
    my_list = []
    elements_list = [14, 52, 14, 12, 43]
    for data in (elements_list[i] for i in range(0, len(elements_list))):
        my_list.append(data)


if __name__ == '__main__':
    import timeit

    print(timeit.timeit("fun1()", setup="from __main__ import fun1", number=100000))
    print(timeit.timeit("fun2()", setup="from __main__ import fun2", number=100000))

output:
0.087008857
0.119698832

Copy link
Contributor Author

@shermanhui shermanhui Oct 5, 2020

Choose a reason for hiding this comment

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

Thanks, good catch, @shellhub. That was informative! I only ran timeit against the list slicing. I should have checked the regular iteration as well. I guess it's also better to keep it simple! 😃

Copy link

Choose a reason for hiding this comment

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

How we Iris dataset from an online source. Read the dataset into a data structure, preferably
in a 2D array??? Anyone help me please its my assignment

stokhos pushed a commit to stokhos/Python that referenced this pull request Jan 3, 2021
…thms#2792)

* chore: update print_reverse helper method

Use a generator expression instead of slicing
`elements_list` to improve the space and time complexity
of `make_linked_list` to O(1) space and O(n) time
by avoiding the creation a shallow copy of `elements_list`.

* fix: add type checking and argument typing

Add argument typing to all methods in `print_reverse`

Add doctest to helper function `make_linked_list` and
basic edge case tests to `print_reverse`

* test: add `print_reverse` test

Fix doctest syntax and remove edge case tests that are covered
by typed arguments.

Add `print_reverse` test that expects the correct values are printed
out by adding a `test_print_reverse_output` helper function.

* format code

Co-authored-by: shellhub <[email protected]>
Panquesito7 pushed a commit to Panquesito7/Python that referenced this pull request May 13, 2021
…thms#2792)

* chore: update print_reverse helper method

Use a generator expression instead of slicing
`elements_list` to improve the space and time complexity
of `make_linked_list` to O(1) space and O(n) time
by avoiding the creation a shallow copy of `elements_list`.

* fix: add type checking and argument typing

Add argument typing to all methods in `print_reverse`

Add doctest to helper function `make_linked_list` and
basic edge case tests to `print_reverse`

* test: add `print_reverse` test

Fix doctest syntax and remove edge case tests that are covered
by typed arguments.

Add `print_reverse` test that expects the correct values are printed
out by adding a `test_print_reverse_output` helper function.

* format code

Co-authored-by: shellhub <[email protected]>
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