Fix mypy errors for data_structures->linked_list directory files (#4927)

This commit is contained in:
Parth Satodiya 2021-10-07 20:48:23 +05:30 committed by GitHub
parent d654806eae
commit d324f91fe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 19 deletions

View File

@ -6,7 +6,7 @@ Nodes contain data and also may link to other nodes:
- Last node: points to null - Last node: points to null
""" """
from typing import Any from typing import Any, Optional
class Node: class Node:
@ -17,7 +17,7 @@ class Node:
class LinkedList: class LinkedList:
def __init__(self) -> None: def __init__(self) -> None:
self.head = None self.head: Optional[Node] = None
self.size = 0 self.size = 0
def add(self, item: Any) -> None: def add(self, item: Any) -> None:
@ -25,7 +25,10 @@ class LinkedList:
self.size += 1 self.size += 1
def remove(self) -> Any: def remove(self) -> Any:
if self.is_empty(): # Switched 'self.is_empty()' to 'self.head is None'
# because mypy was considering the possibility that 'self.head'
# can be None in below else part and giving error
if self.head is None:
return None return None
else: else:
item = self.head.item item = self.head.item
@ -50,7 +53,7 @@ class LinkedList:
else: else:
iterate = self.head iterate = self.head
item_str = "" item_str = ""
item_list = [] item_list: list[str] = []
while iterate: while iterate:
item_list.append(str(iterate.item)) item_list.append(str(iterate.item))
iterate = iterate.next iterate = iterate.next

View File

@ -1,10 +1,10 @@
from typing import Any from typing import Any, Iterator, Optional
class Node: class Node:
def __init__(self, data: Any): def __init__(self, data: Any):
self.data = data self.data: Any = data
self.next = None self.next: Optional[Node] = None
class CircularLinkedList: class CircularLinkedList:
@ -12,7 +12,7 @@ class CircularLinkedList:
self.head = None self.head = None
self.tail = None self.tail = None
def __iter__(self): def __iter__(self) -> Iterator[Any]:
node = self.head node = self.head
while self.head: while self.head:
yield node.data yield node.data
@ -54,10 +54,10 @@ class CircularLinkedList:
def delete_front(self): def delete_front(self):
return self.delete_nth(0) return self.delete_nth(0)
def delete_tail(self) -> None: def delete_tail(self) -> Any:
return self.delete_nth(len(self) - 1) return self.delete_nth(len(self) - 1)
def delete_nth(self, index: int = 0): def delete_nth(self, index: int = 0) -> Any:
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 delete_node = self.head
@ -76,7 +76,7 @@ class CircularLinkedList:
self.tail = temp self.tail = temp
return delete_node.data return delete_node.data
def is_empty(self): def is_empty(self) -> bool:
return len(self) == 0 return len(self) == 0

View File

@ -1,4 +1,4 @@
from typing import Any from typing import Any, Optional
class ContainsLoopError(Exception): class ContainsLoopError(Exception):
@ -7,8 +7,8 @@ class ContainsLoopError(Exception):
class Node: class Node:
def __init__(self, data: Any) -> None: def __init__(self, data: Any) -> None:
self.data = data self.data: Any = data
self.next_node = None self.next_node: Optional[Node] = None
def __iter__(self): def __iter__(self):
node = self node = self

View File

@ -1,5 +1,8 @@
from typing import Optional
class Node: class Node:
def __init__(self, data: int) -> int: def __init__(self, data: int) -> None:
self.data = data self.data = data
self.next = None self.next = None
@ -14,7 +17,7 @@ class LinkedList:
self.head = new_node self.head = new_node
return self.head.data return self.head.data
def middle_element(self) -> int: def middle_element(self) -> Optional[int]:
""" """
>>> link = LinkedList() >>> link = LinkedList()
>>> link.middle_element() >>> link.middle_element()
@ -54,6 +57,7 @@ class LinkedList:
return slow_pointer.data return slow_pointer.data
else: else:
print("No element found.") print("No element found.")
return None
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -5,14 +5,14 @@ https://epaperpress.com/sortsearch/download/skiplist.pdf
from __future__ import annotations from __future__ import annotations
from random import random from random import random
from typing import Generic, TypeVar from typing import Generic, Optional, TypeVar, Union
KT = TypeVar("KT") KT = TypeVar("KT")
VT = TypeVar("VT") VT = TypeVar("VT")
class Node(Generic[KT, VT]): class Node(Generic[KT, VT]):
def __init__(self, key: KT, value: VT): def __init__(self, key: Union[KT, str] = "root", value: Optional[VT] = None):
self.key = key self.key = key
self.value = value self.value = value
self.forward: list[Node[KT, VT]] = [] self.forward: list[Node[KT, VT]] = []
@ -49,7 +49,7 @@ class Node(Generic[KT, VT]):
class SkipList(Generic[KT, VT]): class SkipList(Generic[KT, VT]):
def __init__(self, p: float = 0.5, max_level: int = 16): def __init__(self, p: float = 0.5, max_level: int = 16):
self.head = Node("root", None) self.head: Node[KT, VT] = Node[KT, VT]()
self.level = 0 self.level = 0
self.p = p self.p = p
self.max_level = max_level self.max_level = max_level