mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-16 03:59:18 +08:00
refactor: Follow the PEP 585 Typing standard (#439)
* Follow the PEP 585 Typing standard * Update list.py
This commit is contained in:
@@ -1,12 +1,8 @@
|
||||
import copy
|
||||
import math
|
||||
import heapq
|
||||
import queue
|
||||
import random
|
||||
import functools
|
||||
import collections
|
||||
from typing import Optional, Tuple, List, Dict, DefaultDict, OrderedDict, Set, Deque
|
||||
# Follow the PEP 585 – Type Hinting Generics In Standard Collections
|
||||
# https://peps.python.org/pep-0585/
|
||||
from __future__ import annotations
|
||||
# Import common libs here to simplify the codes by `from module import *`
|
||||
from .linked_list import ListNode, list_to_linked_list, linked_list_to_list, get_list_node
|
||||
from .binary_tree import TreeNode, list_to_tree, tree_to_list, get_tree_node
|
||||
from .binary_tree import TreweNode, list_to_tree, tree_to_list, get_tree_node
|
||||
from .vertex import Vertex, vals_to_vets, vets_to_vals
|
||||
from .print_util import print_matrix, print_linked_list, print_tree, print_dict, print_heap
|
||||
from .print_util import print_matrix, print_linked_list, print_tree, print_dict, print_heap
|
||||
|
||||
@@ -4,25 +4,24 @@ Created Time: 2021-12-11
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import collections
|
||||
from typing import List, Deque, Optional
|
||||
from collections import deque
|
||||
|
||||
class TreeNode:
|
||||
""" Definition for a binary tree node """
|
||||
def __init__(self, val: int = 0, left: Optional['TreeNode'] = None, right: Optional['TreeNode'] = None):
|
||||
self.val: int = val # 结点值
|
||||
self.height: int = 0 # 结点高度
|
||||
self.left: Optional[TreeNode] = left # 左子结点引用
|
||||
self.right: Optional[TreeNode] = right # 右子结点引用
|
||||
def __init__(self, val: int = 0):
|
||||
self.val: int = val # 结点值
|
||||
self.height: int = 0 # 结点高度
|
||||
self.left: TreeNode | None = None # 左子结点引用
|
||||
self.right: TreeNode | None = None # 右子结点引用
|
||||
|
||||
def list_to_tree(arr: List[int]) -> Optional[TreeNode]:
|
||||
def list_to_tree(arr: list[int]) -> TreeNode | None:
|
||||
""" Generate a binary tree with a list """
|
||||
if not arr:
|
||||
return None
|
||||
|
||||
i: int = 0
|
||||
root = TreeNode(arr[0])
|
||||
queue: Deque[TreeNode] = collections.deque([root])
|
||||
queue: deque[TreeNode] = deque([root])
|
||||
while queue:
|
||||
node: TreeNode = queue.popleft()
|
||||
i += 1
|
||||
@@ -38,14 +37,14 @@ def list_to_tree(arr: List[int]) -> Optional[TreeNode]:
|
||||
|
||||
return root
|
||||
|
||||
def tree_to_list(root: Optional[TreeNode]) -> List[int]:
|
||||
def tree_to_list(root: TreeNode | None) -> list[int]:
|
||||
""" Serialize a tree into an array """
|
||||
if not root: return []
|
||||
queue: Deque[TreeNode] = collections.deque()
|
||||
queue: deque[TreeNode] = deque()
|
||||
queue.append(root)
|
||||
res: List[int] = []
|
||||
res: list[int] = []
|
||||
while queue:
|
||||
node: Optional[TreeNode] = queue.popleft()
|
||||
node: TreeNode | None = queue.popleft()
|
||||
if node:
|
||||
res.append(node.val)
|
||||
queue.append(node.left)
|
||||
@@ -53,12 +52,12 @@ def tree_to_list(root: Optional[TreeNode]) -> List[int]:
|
||||
else: res.append(None)
|
||||
return res
|
||||
|
||||
def get_tree_node(root: Optional[TreeNode], val: int) -> Optional[TreeNode]:
|
||||
def get_tree_node(root: TreeNode | None, val: int) -> TreeNode | None:
|
||||
""" Get a tree node with specific value in a binary tree """
|
||||
if not root:
|
||||
return
|
||||
if root.val == val:
|
||||
return root
|
||||
left: Optional[TreeNode] = get_tree_node(root.left, val)
|
||||
right: Optional[TreeNode] = get_tree_node(root.right, val)
|
||||
left: TreeNode | None = get_tree_node(root.left, val)
|
||||
right: TreeNode | None = get_tree_node(root.right, val)
|
||||
return left if left else right
|
||||
|
||||
@@ -4,15 +4,13 @@ Created Time: 2021-12-11
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
from typing import List, Optional
|
||||
|
||||
class ListNode:
|
||||
""" Definition for a singly-linked list node """
|
||||
def __init__(self, val: int):
|
||||
self.val: int = val # 结点值
|
||||
self.next: Optional[ListNode] = None # 后继结点引用
|
||||
self.next: ListNode | None = None # 后继结点引用
|
||||
|
||||
def list_to_linked_list(arr: List[int]) -> Optional[ListNode]:
|
||||
def list_to_linked_list(arr: list[int]) -> ListNode | None:
|
||||
""" Generate a linked list with a list """
|
||||
dum = head = ListNode(0)
|
||||
for a in arr:
|
||||
@@ -21,15 +19,15 @@ def list_to_linked_list(arr: List[int]) -> Optional[ListNode]:
|
||||
head = head.next
|
||||
return dum.next
|
||||
|
||||
def linked_list_to_list(head: Optional[ListNode]) -> List[int]:
|
||||
def linked_list_to_list(head: ListNode | None) -> list[int]:
|
||||
""" Serialize a linked list into an array """
|
||||
arr: List[int] = []
|
||||
arr: list[int] = []
|
||||
while head:
|
||||
arr.append(head.val)
|
||||
head = head.next
|
||||
return arr
|
||||
|
||||
def get_list_node(head: Optional[ListNode], val: int) -> Optional[ListNode]:
|
||||
def get_list_node(head: ListNode | None, val: int) -> ListNode | None:
|
||||
""" Get a list node with specific value from a linked list """
|
||||
while head and head.val != val:
|
||||
head = head.next
|
||||
|
||||
@@ -7,33 +7,31 @@ Author: Krahets (krahets@163.com), msk397 (machangxinq@gmail.com)
|
||||
from .binary_tree import TreeNode, list_to_tree
|
||||
from .linked_list import ListNode, linked_list_to_list
|
||||
|
||||
from typing import List, Optional, Dict
|
||||
|
||||
def print_matrix(mat: List[List[int]]) -> None:
|
||||
def print_matrix(mat: list[list[int]]) -> None:
|
||||
""" Print a matrix """
|
||||
s: List[str] = []
|
||||
s: list[str] = []
|
||||
for arr in mat:
|
||||
s.append(' ' + str(arr))
|
||||
|
||||
print('[\n' + ',\n'.join(s) + '\n]')
|
||||
|
||||
def print_linked_list(head: Optional[ListNode]) -> None:
|
||||
def print_linked_list(head: ListNode | None) -> None:
|
||||
""" Print a linked list """
|
||||
arr: List[int] = linked_list_to_list(head)
|
||||
arr: list[int] = linked_list_to_list(head)
|
||||
print(' -> '.join([str(a) for a in arr]))
|
||||
|
||||
class Trunk:
|
||||
def __init__(self, prev: Optional['Trunk'] = None, string: Optional[str] = None) -> None:
|
||||
self.prev: Optional[Trunk] = prev
|
||||
self.str: Optional[str] = string
|
||||
def __init__(self, prev, string: str | None = None) -> None:
|
||||
self.prev = prev
|
||||
self.str = string
|
||||
|
||||
def show_trunks(p: Optional[Trunk]) -> None:
|
||||
def show_trunks(p: Trunk | None) -> None:
|
||||
if p is None:
|
||||
return
|
||||
show_trunks(p.prev)
|
||||
print(p.str, end='')
|
||||
|
||||
def print_tree(root: Optional[TreeNode], prev: Optional[Trunk] = None, is_left: bool = False) -> None:
|
||||
def print_tree(root: TreeNode | None, prev: Trunk | None = None, is_left: bool = False) -> None:
|
||||
"""
|
||||
Print a binary tree
|
||||
This tree printer is borrowed from TECHIE DELIGHT
|
||||
@@ -62,14 +60,14 @@ def print_tree(root: Optional[TreeNode], prev: Optional[Trunk] = None, is_left:
|
||||
trunk.str = ' |'
|
||||
print_tree(root.left, trunk, False)
|
||||
|
||||
def print_dict(mapp: Dict) -> None:
|
||||
def print_dict(mapp: dict) -> None:
|
||||
""" Print a dict """
|
||||
for key, value in mapp.items():
|
||||
print(key, '->', value)
|
||||
|
||||
def print_heap(heap: List[int]) -> None:
|
||||
def print_heap(heap: list[int]) -> None:
|
||||
""" Print a heap both in array and tree representations """
|
||||
print("堆的数组表示:", heap)
|
||||
print("堆的树状表示:")
|
||||
root: Optional[TreeNode] = list_to_tree(heap)
|
||||
root: TreeNode | None = list_to_tree(heap)
|
||||
print_tree(root)
|
||||
|
||||
@@ -2,17 +2,15 @@
|
||||
# Created Time: 2023-02-23
|
||||
# Author: Krahets (krahets@163.com)
|
||||
|
||||
from typing import List
|
||||
|
||||
class Vertex:
|
||||
""" 顶点类 """
|
||||
def __init__(self, val: int) -> None:
|
||||
self.val = val
|
||||
|
||||
def vals_to_vets(vals: List[int]) -> List['Vertex']:
|
||||
def vals_to_vets(vals: list[int]) -> list['Vertex']:
|
||||
""" 输入值列表 vals ,返回顶点列表 vets """
|
||||
return [Vertex(val) for val in vals]
|
||||
|
||||
def vets_to_vals(vets: List['Vertex']) -> List[int]:
|
||||
def vets_to_vals(vets: list['Vertex']) -> list[int]:
|
||||
""" 输入顶点列表 vets ,返回值列表 vals """
|
||||
return [vet.val for vet in vets]
|
||||
|
||||
Reference in New Issue
Block a user