mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
refactor: Follow the PEP 585 Typing standard (#439)
* Follow the PEP 585 Typing standard * Update list.py
This commit is contained in:
@@ -4,11 +4,9 @@ Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from modules import *
|
||||
import random
|
||||
|
||||
def random_access(nums: List[int]) -> int:
|
||||
def random_access(nums: list[int]) -> int:
|
||||
""" 随机访问元素 """
|
||||
# 在区间 [0, len(nums)-1] 中随机抽取一个数字
|
||||
random_index = random.randint(0, len(nums) - 1)
|
||||
@@ -18,7 +16,7 @@ def random_access(nums: List[int]) -> int:
|
||||
|
||||
# 请注意,Python 的 list 是动态数组,可以直接扩展
|
||||
# 为了方便学习,本函数将 list 看作是长度不可变的数组
|
||||
def extend(nums: List[int], enlarge: int) -> List[int]:
|
||||
def extend(nums: list[int], enlarge: int) -> list[int]:
|
||||
""" 扩展数组长度 """
|
||||
# 初始化一个扩展长度后的数组
|
||||
res = [0] * (len(nums) + enlarge)
|
||||
@@ -28,7 +26,7 @@ def extend(nums: List[int], enlarge: int) -> List[int]:
|
||||
# 返回扩展后的新数组
|
||||
return res
|
||||
|
||||
def insert(nums: List[int], num: int, index: int) -> None:
|
||||
def insert(nums: list[int], num: int, index: int) -> None:
|
||||
""" 在数组的索引 index 处插入元素 num """
|
||||
# 把索引 index 以及之后的所有元素向后移动一位
|
||||
for i in range(len(nums) - 1, index, -1):
|
||||
@@ -36,13 +34,13 @@ def insert(nums: List[int], num: int, index: int) -> None:
|
||||
# 将 num 赋给 index 处元素
|
||||
nums[index] = num
|
||||
|
||||
def remove(nums: List[int], index: int) -> None:
|
||||
def remove(nums: list[int], index: int) -> None:
|
||||
""" 删除索引 index 处元素 """
|
||||
# 把索引 index 之后的所有元素向前移动一位
|
||||
for i in range(index, len(nums) - 1):
|
||||
nums[i] = nums[i + 1]
|
||||
|
||||
def traverse(nums: List[int]) -> None:
|
||||
def traverse(nums: list[int]) -> None:
|
||||
""" 遍历数组 """
|
||||
count = 0
|
||||
# 通过索引遍历数组
|
||||
@@ -52,7 +50,7 @@ def traverse(nums: List[int]) -> None:
|
||||
for num in nums:
|
||||
count += 1
|
||||
|
||||
def find(nums: List[int], target: int) -> int:
|
||||
def find(nums: list[int], target: int) -> int:
|
||||
""" 在数组中查找指定元素 """
|
||||
for i in range(len(nums)):
|
||||
if nums[i] == target:
|
||||
@@ -62,9 +60,9 @@ def find(nums: List[int], target: int) -> int:
|
||||
""" Driver Code """
|
||||
if __name__ == "__main__":
|
||||
""" 初始化数组 """
|
||||
arr: List[int] = [0] * 5
|
||||
arr: list[int] = [0] * 5
|
||||
print("数组 arr =", arr)
|
||||
nums: List[int] = [1, 3, 2, 5, 4]
|
||||
nums: list[int] = [1, 3, 2, 5, 4]
|
||||
print("数组 nums =", nums)
|
||||
|
||||
""" 随机访问 """
|
||||
@@ -72,7 +70,7 @@ if __name__ == "__main__":
|
||||
print("在 nums 中获取随机元素", random_num)
|
||||
|
||||
""" 长度扩展 """
|
||||
nums: List[int] = extend(nums, 3)
|
||||
nums: list[int] = extend(nums, 3)
|
||||
print("将数组长度扩展至 8 ,得到 nums =", nums)
|
||||
|
||||
""" 插入元素 """
|
||||
|
||||
@@ -23,7 +23,7 @@ def remove(n0: ListNode) -> None:
|
||||
n1 = P.next
|
||||
n0.next = n1
|
||||
|
||||
def access(head: ListNode, index: int) -> Optional[ListNode]:
|
||||
def access(head: ListNode, index: int) -> ListNode | None:
|
||||
""" 访问链表中索引为 index 的结点 """
|
||||
for _ in range(index):
|
||||
if not head:
|
||||
|
||||
@@ -4,60 +4,55 @@ Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from modules import *
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
if __name__ == "__main__":
|
||||
""" 初始化列表 """
|
||||
list: List[int] = [1, 3, 2, 5, 4]
|
||||
print("列表 list =", list)
|
||||
arr: list[int] = [1, 3, 2, 5, 4]
|
||||
print("列表 arr =", arr)
|
||||
|
||||
""" 访问元素 """
|
||||
num: int = list[1]
|
||||
num: int = arr[1]
|
||||
print("访问索引 1 处的元素,得到 num =", num)
|
||||
|
||||
""" 更新元素 """
|
||||
list[1] = 0
|
||||
print("将索引 1 处的元素更新为 0 ,得到 list =", list)
|
||||
arr[1] = 0
|
||||
print("将索引 1 处的元素更新为 0 ,得到 arr =", arr)
|
||||
|
||||
""" 清空列表 """
|
||||
list.clear()
|
||||
print("清空列表后 list =", list)
|
||||
arr.clear()
|
||||
print("清空列表后 arr =", arr)
|
||||
|
||||
""" 尾部添加元素 """
|
||||
list.append(1)
|
||||
list.append(3)
|
||||
list.append(2)
|
||||
list.append(5)
|
||||
list.append(4)
|
||||
print("添加元素后 list =", list)
|
||||
arr.append(1)
|
||||
arr.append(3)
|
||||
arr.append(2)
|
||||
arr.append(5)
|
||||
arr.append(4)
|
||||
print("添加元素后 arr =", arr)
|
||||
|
||||
""" 中间插入元素 """
|
||||
list.insert(3, 6)
|
||||
print("在索引 3 处插入数字 6 ,得到 list =", list)
|
||||
arr.insert(3, 6)
|
||||
print("在索引 3 处插入数字 6 ,得到 arr =", arr)
|
||||
|
||||
""" 删除元素 """
|
||||
list.pop(3)
|
||||
print("删除索引 3 处的元素,得到 list =", list)
|
||||
arr.pop(3)
|
||||
print("删除索引 3 处的元素,得到 arr =", arr)
|
||||
|
||||
""" 通过索引遍历列表 """
|
||||
count: int = 0
|
||||
for i in range(len(list)):
|
||||
for i in range(len(arr)):
|
||||
count += 1
|
||||
|
||||
""" 直接遍历列表元素 """
|
||||
count: int = 0
|
||||
for n in list:
|
||||
for n in arr:
|
||||
count += 1
|
||||
|
||||
""" 拼接两个列表 """
|
||||
list1: List[int] = [6, 8, 7, 10, 9]
|
||||
list += list1
|
||||
print("将列表 list1 拼接到 list 之后,得到 list =", list)
|
||||
arr1: list[int] = [6, 8, 7, 10, 9]
|
||||
arr += arr1
|
||||
print("将列表 arr1 拼接到 arr 之后,得到 arr =", arr)
|
||||
|
||||
""" 排序列表 """
|
||||
list.sort()
|
||||
print("排序列表后 list =", list)
|
||||
arr.sort()
|
||||
print("排序列表后 arr =", arr)
|
||||
|
||||
@@ -4,16 +4,12 @@ Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import sys, os.path as osp
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from modules import *
|
||||
|
||||
class MyList:
|
||||
""" 列表类简易实现 """
|
||||
def __init__(self):
|
||||
""" 构造方法 """
|
||||
self.__capacity: int = 10 # 列表容量
|
||||
self.__nums: List[int] = [0] * self.__capacity # 数组(存储列表元素)
|
||||
self.__nums: my_list[int] = [0] * self.__capacity # 数组(存储列表元素)
|
||||
self.__size: int = 0 # 列表长度(即当前元素数量)
|
||||
self.__extend_ratio: int = 2 # 每次列表扩容的倍数
|
||||
|
||||
@@ -76,7 +72,7 @@ class MyList:
|
||||
# 更新列表容量
|
||||
self.__capacity = len(self.__nums)
|
||||
|
||||
def to_array(self) -> List[int]:
|
||||
def to_array(self) -> list[int]:
|
||||
""" 返回有效长度的列表 """
|
||||
return self.__nums[:self.__size]
|
||||
|
||||
@@ -84,35 +80,35 @@ class MyList:
|
||||
""" Driver Code """
|
||||
if __name__ == "__main__":
|
||||
""" 初始化列表 """
|
||||
list = MyList()
|
||||
my_list = MyList()
|
||||
""" 尾部添加元素 """
|
||||
list.add(1)
|
||||
list.add(3)
|
||||
list.add(2)
|
||||
list.add(5)
|
||||
list.add(4)
|
||||
print("列表 list = {} ,容量 = {} ,长度 = {}"
|
||||
.format(list.to_array(), list.capacity(), list.size()))
|
||||
my_list.add(1)
|
||||
my_list.add(3)
|
||||
my_list.add(2)
|
||||
my_list.add(5)
|
||||
my_list.add(4)
|
||||
print("列表 my_list = {} ,容量 = {} ,长度 = {}"
|
||||
.format(my_list.to_array(), my_list.capacity(), my_list.size()))
|
||||
|
||||
""" 中间插入元素 """
|
||||
list.insert(6, index=3)
|
||||
print("在索引 3 处插入数字 6 ,得到 list =", list.to_array())
|
||||
my_list.insert(6, index=3)
|
||||
print("在索引 3 处插入数字 6 ,得到 my_list =", my_list.to_array())
|
||||
|
||||
""" 删除元素 """
|
||||
list.remove(3)
|
||||
print("删除索引 3 处的元素,得到 list =", list.to_array())
|
||||
my_list.remove(3)
|
||||
print("删除索引 3 处的元素,得到 my_list =", my_list.to_array())
|
||||
|
||||
""" 访问元素 """
|
||||
num = list.get(1)
|
||||
num = my_list.get(1)
|
||||
print("访问索引 1 处的元素,得到 num =", num)
|
||||
|
||||
""" 更新元素 """
|
||||
list.set(0, 1)
|
||||
print("将索引 1 处的元素更新为 0 ,得到 list =", list.to_array())
|
||||
my_list.set(0, 1)
|
||||
print("将索引 1 处的元素更新为 0 ,得到 my_list =", my_list.to_array())
|
||||
|
||||
""" 测试扩容机制 """
|
||||
for i in range(10):
|
||||
# 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
|
||||
list.add(i)
|
||||
print("扩容后的列表 list = {} ,容量 = {} ,长度 = {}"
|
||||
.format(list.to_array(), list.capacity(), list.size()))
|
||||
my_list.add(i)
|
||||
print("扩容后的列表 my_list = {} ,容量 = {} ,长度 = {}"
|
||||
.format(my_list.to_array(), my_list.capacity(), my_list.size()))
|
||||
|
||||
Reference in New Issue
Block a user