From dfdd78135df938d948ba3044aca628aca08886e7 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Wed, 4 Oct 2023 12:05:00 -0400 Subject: [PATCH] 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> --- DIRECTORY.md | 5 ++--- .../linked_list/circular_linked_list.py | 22 +++++++++++++------ data_structures/linked_list/swap_nodes.py | 4 ++-- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9a913aa78..4f4cc423d 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) @@ -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) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index 72212f46b..ef6658733 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.py @@ -20,8 +20,8 @@ class CircularLinkedList: """ 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]: """ @@ -30,7 +30,7 @@ class CircularLinkedList: 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: @@ -74,17 +74,20 @@ class CircularLinkedList: """ 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 @@ -120,16 +123,21 @@ class CircularLinkedList: """ 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 diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index da6aa07a7..31dcb02bf 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -11,7 +11,7 @@ class Node: """ self.data = data - self.next = None # Reference to the next node + self.next: Node | None = None # Reference to the next node class LinkedList: @@ -19,7 +19,7 @@ class LinkedList: """ 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): """