Skip to content

Added an add at position subroutiune to linked list #9020

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

Merged
merged 14 commits into from
Sep 8, 2023

Conversation

mendacium-a11y
Copy link
Contributor

Describe your change:

This pull request adds the add_at_position function to the linked list implementation. This function allows inserting an item at a specified position within the linked list.

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 include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@mendacium-a11y mendacium-a11y changed the title Linked list Added an add at position subroutiune to linked list Aug 30, 2023
>>> linked_list.add(3)
>>> linked_list.add_at_position(10, 1)
True

Copy link
Contributor

Choose a reason for hiding this comment

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

Here there should be a test displaying linked_list

current = current.next
counter += 1

if current: # Check if current is not None
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if current: # Check if current is not None
if current is not None:

Comment on lines 60 to 61
else:
return False
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
else:
return False
return False

Comment on lines 47 to 48
if position < 0:
return False
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe this should raise an error?

Suggested change
if position < 0:
return False
if 0 > position:
return False

This also looks a bit more readable

@@ -25,6 +25,41 @@ def add(self, item: Any) -> None:
self.head = Node(item, self.head)
self.size += 1

def add_at_position(self, item: Any, position: int) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

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

What about insert_at_position?

@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels Sep 5, 2023
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Sep 6, 2023
@mendacium-a11y
Copy link
Contributor Author

updated the the code by dropping the add_at_position, instead updated the add function to take an optional position argument to specify of we want to insert the node at a specific position in the linked list

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Sep 6, 2023
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Sep 6, 2023
CONTRIBUTING.md Outdated
@@ -1,4 +1,4 @@
# Contributing guidelines
<!-- # Contributing guidelines -->
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't edit the contributing guidelines

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this seems to have happened by mistake, didn't intend to

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
<!-- # Contributing guidelines -->
# Contributing guidelines

Then please uncomment this line

@mendacium-a11y
Copy link
Contributor Author

added the doctest and updated the else code after checking the position is 0 or not

CONTRIBUTING.md Outdated
@@ -1,4 +1,4 @@
# Contributing guidelines
<!-- # Contributing guidelines -->
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
<!-- # Contributing guidelines -->
# Contributing guidelines

Then please uncomment this line

@mendacium-a11y
Copy link
Contributor Author

fixed contributing.md and added the doctest for checing out of bounds posiiton, both positive and negative

@tianyizheng02 tianyizheng02 merged commit c9b4b80 into TheAlgorithms:master Sep 8, 2023
@tianyizheng02
Copy link
Contributor

Thanks for your contribution!

@mendacium-a11y mendacium-a11y deleted the linkedList branch September 9, 2023 07:02
sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
* added addAtPosition to simple linked list

* added addAtPosition to simple linked list

* modified the add function to take an optional position command

* fixed type safety errors:

* fixed type safety errors:

* fixed type safety errors:

* fixed type safety errors:

* fixed size error

* fixed size error

* added doctest and updates the else after checking if posiiton argument less than 0 or not

* added doctest and updates the else after checking if posiiton argument less than 0 or not

* fixed the contributing.md mistake

* added doctest for out of bounds position value, both negative and positive
@isidroas isidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants