Skip to content

Fix mypy errors in circular_linked_list.py and swap_nodes.py #9707

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 5 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,8 @@
* [Basic Maths](maths/basic_maths.py)
* [Binary Exp Mod](maths/binary_exp_mod.py)
* [Binary Exponentiation](maths/binary_exponentiation.py)
* [Binary Exponentiation 2](maths/binary_exponentiation_2.py)
* [Binary Exponentiation 3](maths/binary_exponentiation_3.py)
* [Binary Multiplication](maths/binary_multiplication.py)
* [Binomial Coefficient](maths/binomial_coefficient.py)
* [Binomial Distribution](maths/binomial_distribution.py)
* [Bisection](maths/bisection.py)
Expand All @@ -557,8 +557,7 @@
* [Decimal Isolate](maths/decimal_isolate.py)
* [Decimal To Fraction](maths/decimal_to_fraction.py)
* [Dodecahedron](maths/dodecahedron.py)
* [Double Factorial Iterative](maths/double_factorial_iterative.py)
* [Double Factorial Recursive](maths/double_factorial_recursive.py)
* [Double Factorial](maths/double_factorial.py)
* [Dual Number Automatic Differentiation](maths/dual_number_automatic_differentiation.py)
* [Entropy](maths/entropy.py)
* [Euclidean Distance](maths/euclidean_distance.py)
Expand Down
22 changes: 15 additions & 7 deletions data_structures/linked_list/circular_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def __init__(self) -> None:
"""
Initialize an empty Circular Linked List.
"""
self.head = None # Reference to the head (first node)
self.tail = None # Reference to the tail (last node)
self.head: Node | None = None # Reference to the head (first node)
self.tail: Node | None = None # Reference to the tail (last node)

def __iter__(self) -> Iterator[Any]:
"""
Expand All @@ -30,7 +30,7 @@ def __iter__(self) -> Iterator[Any]:
The data of each node in the linked list.
"""
node = self.head
while self.head:
while node:
yield node.data
node = node.next
if node == self.head:
Expand Down Expand Up @@ -74,17 +74,20 @@ def insert_nth(self, index: int, data: Any) -> None:
"""
if index < 0 or index > len(self):
raise IndexError("list index out of range.")
new_node = Node(data)
new_node: Node = Node(data)
if self.head is None:
new_node.next = new_node # First node points to itself
self.tail = self.head = new_node
elif index == 0: # Insert at the head
new_node.next = self.head
assert self.tail is not None # List is not empty, tail exists
self.head = self.tail.next = new_node
else:
temp = self.head
temp: Node | None = self.head
for _ in range(index - 1):
assert temp is not None
temp = temp.next
assert temp is not None
new_node.next = temp.next
temp.next = new_node
if index == len(self) - 1: # Insert at the tail
Expand Down Expand Up @@ -120,16 +123,21 @@ def delete_nth(self, index: int = 0) -> Any:
"""
if not 0 <= index < len(self):
raise IndexError("list index out of range.")
delete_node = self.head

assert self.head is not None and self.tail is not None
delete_node: Node = self.head
if self.head == self.tail: # Just one node
self.head = self.tail = None
elif index == 0: # Delete head node
assert self.tail.next is not None
self.tail.next = self.tail.next.next
self.head = self.head.next
else:
temp = self.head
temp: Node | None = self.head
for _ in range(index - 1):
assert temp is not None
temp = temp.next
assert temp is not None and temp.next is not None
delete_node = temp.next
temp.next = temp.next.next
if index == len(self) - 1: # Delete at tail
Expand Down
4 changes: 2 additions & 2 deletions data_structures/linked_list/swap_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ def __init__(self, data: Any) -> None:
"""
self.data = data
self.next = None # Reference to the next node
self.next: Node | None = None # Reference to the next node


class LinkedList:
def __init__(self) -> None:
"""
Initialize an empty Linked List.
"""
self.head = None # Reference to the head (first node)
self.head: Node | None = None # Reference to the head (first node)

def print_list(self):
"""
Expand Down