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

* updating DIRECTORY.md

* Fix mypy errors in circular_linked_list.py

* Fix mypy errors in swap_nodes.py

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Tianyi Zheng 2023-10-04 12:05:00 -04:00 committed by GitHub
parent 3fd3497f15
commit dfdd78135d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 12 deletions

View File

@ -541,8 +541,8 @@
* [Basic Maths](maths/basic_maths.py) * [Basic Maths](maths/basic_maths.py)
* [Binary Exp Mod](maths/binary_exp_mod.py) * [Binary Exp Mod](maths/binary_exp_mod.py)
* [Binary Exponentiation](maths/binary_exponentiation.py) * [Binary Exponentiation](maths/binary_exponentiation.py)
* [Binary Exponentiation 2](maths/binary_exponentiation_2.py)
* [Binary Exponentiation 3](maths/binary_exponentiation_3.py) * [Binary Exponentiation 3](maths/binary_exponentiation_3.py)
* [Binary Multiplication](maths/binary_multiplication.py)
* [Binomial Coefficient](maths/binomial_coefficient.py) * [Binomial Coefficient](maths/binomial_coefficient.py)
* [Binomial Distribution](maths/binomial_distribution.py) * [Binomial Distribution](maths/binomial_distribution.py)
* [Bisection](maths/bisection.py) * [Bisection](maths/bisection.py)
@ -557,8 +557,7 @@
* [Decimal Isolate](maths/decimal_isolate.py) * [Decimal Isolate](maths/decimal_isolate.py)
* [Decimal To Fraction](maths/decimal_to_fraction.py) * [Decimal To Fraction](maths/decimal_to_fraction.py)
* [Dodecahedron](maths/dodecahedron.py) * [Dodecahedron](maths/dodecahedron.py)
* [Double Factorial Iterative](maths/double_factorial_iterative.py) * [Double Factorial](maths/double_factorial.py)
* [Double Factorial Recursive](maths/double_factorial_recursive.py)
* [Dual Number Automatic Differentiation](maths/dual_number_automatic_differentiation.py) * [Dual Number Automatic Differentiation](maths/dual_number_automatic_differentiation.py)
* [Entropy](maths/entropy.py) * [Entropy](maths/entropy.py)
* [Euclidean Distance](maths/euclidean_distance.py) * [Euclidean Distance](maths/euclidean_distance.py)

View File

@ -20,8 +20,8 @@ class CircularLinkedList:
""" """
Initialize an empty Circular Linked List. Initialize an empty Circular Linked List.
""" """
self.head = None # Reference to the head (first node) self.head: Node | None = None # Reference to the head (first node)
self.tail = None # Reference to the tail (last node) self.tail: Node | None = None # Reference to the tail (last node)
def __iter__(self) -> Iterator[Any]: def __iter__(self) -> Iterator[Any]:
""" """
@ -30,7 +30,7 @@ class CircularLinkedList:
The data of each node in the linked list. The data of each node in the linked list.
""" """
node = self.head node = self.head
while self.head: while node:
yield node.data yield node.data
node = node.next node = node.next
if node == self.head: if node == self.head:
@ -74,17 +74,20 @@ class CircularLinkedList:
""" """
if index < 0 or index > len(self): if index < 0 or index > len(self):
raise IndexError("list index out of range.") raise IndexError("list index out of range.")
new_node = Node(data) new_node: Node = Node(data)
if self.head is None: if self.head is None:
new_node.next = new_node # First node points to itself new_node.next = new_node # First node points to itself
self.tail = self.head = new_node self.tail = self.head = new_node
elif index == 0: # Insert at the head elif index == 0: # Insert at the head
new_node.next = self.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 self.head = self.tail.next = new_node
else: else:
temp = self.head temp: Node | None = self.head
for _ in range(index - 1): for _ in range(index - 1):
assert temp is not None
temp = temp.next temp = temp.next
assert temp is not None
new_node.next = temp.next new_node.next = temp.next
temp.next = new_node temp.next = new_node
if index == len(self) - 1: # Insert at the tail if index == len(self) - 1: # Insert at the tail
@ -120,16 +123,21 @@ class CircularLinkedList:
""" """
if not 0 <= index < len(self): if not 0 <= index < len(self):
raise IndexError("list index out of range.") 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 if self.head == self.tail: # Just one node
self.head = self.tail = None self.head = self.tail = None
elif index == 0: # Delete head node elif index == 0: # Delete head node
assert self.tail.next is not None
self.tail.next = self.tail.next.next self.tail.next = self.tail.next.next
self.head = self.head.next self.head = self.head.next
else: else:
temp = self.head temp: Node | None = self.head
for _ in range(index - 1): for _ in range(index - 1):
assert temp is not None
temp = temp.next temp = temp.next
assert temp is not None and temp.next is not None
delete_node = temp.next delete_node = temp.next
temp.next = temp.next.next temp.next = temp.next.next
if index == len(self) - 1: # Delete at tail if index == len(self) - 1: # Delete at tail

View File

@ -11,7 +11,7 @@ class Node:
""" """
self.data = data self.data = data
self.next = None # Reference to the next node self.next: Node | None = None # Reference to the next node
class LinkedList: class LinkedList:
@ -19,7 +19,7 @@ class LinkedList:
""" """
Initialize an empty Linked List. 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): def print_list(self):
""" """