Format python codes with black. (#453)

This commit is contained in:
Yudong Jin
2023-04-09 05:05:35 +08:00
committed by GitHub
parent 1c8b7ef559
commit 5ddcb60825
45 changed files with 656 additions and 456 deletions

View File

@ -1,8 +1,20 @@
# 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 .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 .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,
)

View File

@ -6,40 +6,47 @@ Author: Krahets (krahets@163.com)
from collections import deque
class TreeNode:
""" Definition for a binary tree node """
"""Definition for a binary tree node"""
def __init__(self, val: int = 0):
self.val: int = val # 节点值
self.height: int = 0 # 节点高度
self.val: int = val # 节点值
self.height: int = 0 # 节点高度
self.left: TreeNode | None = None # 左子节点引用
self.right: TreeNode | None = None # 右子节点引用
self.right: TreeNode | None = None # 右子节点引用
def list_to_tree(arr: list[int]) -> TreeNode | None:
""" Generate a binary tree with a list """
"""Generate a binary tree with a list"""
if not arr:
return None
i: int = 0
root = TreeNode(arr[0])
queue: deque[TreeNode] = deque([root])
while queue:
node: TreeNode = queue.popleft()
i += 1
if i >= len(arr): break
if i >= len(arr):
break
if arr[i] != None:
node.left = TreeNode(arr[i])
queue.append(node.left)
i += 1
if i >= len(arr): break
if i >= len(arr):
break
if arr[i] != None:
node.right = TreeNode(arr[i])
queue.append(node.right)
return root
def tree_to_list(root: TreeNode | None) -> list[int]:
""" Serialize a tree into an array """
if not root: return []
"""Serialize a tree into an array"""
if not root:
return []
queue: deque[TreeNode] = deque()
queue.append(root)
res: list[int] = []
@ -49,11 +56,13 @@ def tree_to_list(root: TreeNode | None) -> list[int]:
res.append(node.val)
queue.append(node.left)
queue.append(node.right)
else: res.append(None)
else:
res.append(None)
return res
def get_tree_node(root: TreeNode | None, val: int) -> TreeNode | None:
""" Get a tree node with specific value in a binary tree """
"""Get a tree node with specific value in a binary tree"""
if not root:
return
if root.val == val:

View File

@ -4,14 +4,17 @@ Created Time: 2021-12-11
Author: Krahets (krahets@163.com)
"""
class ListNode:
""" Definition for a singly-linked list node """
"""Definition for a singly-linked list node"""
def __init__(self, val: int):
self.val: int = val # 节点值
self.next: ListNode | None = None # 后继节点引用
self.val: int = val # 节点值
self.next: ListNode | None = None # 后继节点引用
def list_to_linked_list(arr: list[int]) -> ListNode | None:
""" Generate a linked list with a list """
"""Generate a linked list with a list"""
dum = head = ListNode(0)
for a in arr:
node = ListNode(a)
@ -19,16 +22,18 @@ def list_to_linked_list(arr: list[int]) -> ListNode | None:
head = head.next
return dum.next
def linked_list_to_list(head: ListNode | None) -> list[int]:
""" Serialize a linked list into an array """
"""Serialize a linked list into an array"""
arr: list[int] = []
while head:
arr.append(head.val)
head = head.next
return arr
def get_list_node(head: ListNode | None, val: int) -> ListNode | None:
""" Get a list node with specific value from a linked list """
"""Get a list node with specific value from a linked list"""
while head and head.val != val:
head = head.next
return head

View File

@ -7,31 +7,38 @@ 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
def print_matrix(mat: list[list[int]]) -> None:
""" Print a matrix """
"""Print a matrix"""
s: list[str] = []
for arr in mat:
s.append(' ' + str(arr))
s.append(" " + str(arr))
print("[\n" + ",\n".join(s) + "\n]")
print('[\n' + ',\n'.join(s) + '\n]')
def print_linked_list(head: ListNode | None) -> None:
""" Print a linked list """
"""Print a linked list"""
arr: list[int] = linked_list_to_list(head)
print(' -> '.join([str(a) for a in arr]))
print(" -> ".join([str(a) for a in arr]))
class Trunk:
def __init__(self, prev, string: str | None = None) -> None:
self.prev = prev
self.str = string
def show_trunks(p: Trunk | None) -> None:
if p is None:
return
show_trunks(p.prev)
print(p.str, end='')
print(p.str, end="")
def print_tree(root: TreeNode | None, prev: Trunk | None = 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
@ -40,33 +47,35 @@ def print_tree(root: TreeNode | None, prev: Trunk | None = None, is_left: bool =
if root is None:
return
prev_str: str = ' '
prev_str: str = " "
trunk = Trunk(prev, prev_str)
print_tree(root.right, trunk, True)
if prev is None:
trunk.str = '———'
trunk.str = "———"
elif is_left:
trunk.str = '/———'
prev_str = ' |'
trunk.str = "/———"
prev_str = " |"
else:
trunk.str = '\———'
trunk.str = "\———"
prev.str = prev_str
show_trunks(trunk)
print(' ' + str(root.val))
print(" " + str(root.val))
if prev:
prev.str = prev_str
trunk.str = ' |'
trunk.str = " |"
print_tree(root.left, trunk, False)
def print_dict(mapp: dict) -> None:
""" Print a dict """
"""Print a dict"""
for key, value in mapp.items():
print(key, '->', value)
print(key, "->", value)
def print_heap(heap: list[int]) -> None:
""" Print a heap both in array and tree representations """
"""Print a heap both in array and tree representations"""
print("堆的数组表示:", heap)
print("堆的树状表示:")
root: TreeNode | None = list_to_tree(heap)

View File

@ -2,15 +2,19 @@
# Created Time: 2023-02-23
# Author: Krahets (krahets@163.com)
class Vertex:
""" 顶点类 """
"""顶点类"""
def __init__(self, val: int) -> None:
self.val = val
def vals_to_vets(vals: list[int]) -> list['Vertex']:
""" 输入值列表 vals ,返回顶点列表 vets """
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]:
""" 输入顶点列表 vets ,返回值列表 vals """
def vets_to_vals(vets: list["Vertex"]) -> list[int]:
"""输入顶点列表 vets ,返回值列表 vals"""
return [vet.val for vet in vets]