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
"""
from typing import Any
from typing import Any, Optional
class Node:
@ -17,7 +17,7 @@ class Node:
class LinkedList:
def __init__(self) -> None:
self.head = None
self.head: Optional[Node] = None
self.size = 0
def add(self, item: Any) -> None:
@ -25,7 +25,10 @@ class LinkedList:
self.size += 1
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
else:
item = self.head.item
@ -50,7 +53,7 @@ class LinkedList:
else:
iterate = self.head
item_str = ""
item_list = []
item_list: list[str] = []
while iterate:
item_list.append(str(iterate.item))
iterate = iterate.next

View File

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

View File

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

View File

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

View File

@ -5,14 +5,14 @@ https://epaperpress.com/sortsearch/download/skiplist.pdf
from __future__ import annotations
from random import random
from typing import Generic, TypeVar
from typing import Generic, Optional, TypeVar, Union
KT = TypeVar("KT")
VT = TypeVar("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.value = value
self.forward: list[Node[KT, VT]] = []
@ -49,7 +49,7 @@ class Node(Generic[KT, VT]):
class SkipList(Generic[KT, VT]):
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.p = p
self.max_level = max_level