mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-29 17:14:38 +00: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)
|
||||
|
||||
""" 插入元素 """
|
||||
|
||||
Reference in New Issue
Block a user