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

Hacktoberfest: Update Linked List - `print_reverse` method #2792

Merged

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}.
shermanhui added 3 commits Oct 4, 2020
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 shermanhui:update-print-reverse-linked-list branch from c695884 to 4b1e4f8 Oct 5, 2020
@shellhub
Copy link
Member

@shellhub shellhub commented Oct 5, 2020

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

@shellhub shellhub merged commit 477b2c2 into TheAlgorithms:master Oct 5, 2020
3 checks passed
3 checks passed
codespell
Details
pre-commit
Details
Travis CI - Pull Request Build Passed
Details
for data in elements_list[1:]:
current.next = Node(data)
current = head = Node(elements_list[0])
for i in range(1, len(elements_list)):

This comment has been minimized.

@shermanhui

shermanhui Oct 5, 2020
Author Contributor

@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)):

This comment has been minimized.

@shellhub

shellhub Oct 5, 2020
Member

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

This comment has been minimized.

@shellhub

shellhub Oct 5, 2020
Member

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

This comment has been minimized.

@shermanhui

shermanhui Oct 5, 2020
Author Contributor

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! 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.