mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Update variable names in list and my_list
This commit is contained in:
@ -7,52 +7,53 @@ Author: Krahets (krahets@163.com)
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化列表
|
||||
arr = [1, 3, 2, 5, 4]
|
||||
print("列表 arr =", arr)
|
||||
nums: list[int] = [1, 3, 2, 5, 4]
|
||||
print("\n列表 nums =", nums)
|
||||
|
||||
# 访问元素
|
||||
num: int = arr[1]
|
||||
print("访问索引 1 处的元素,得到 num =", num)
|
||||
x: int = nums[1]
|
||||
print("\n访问索引 1 处的元素,得到 x =", x)
|
||||
|
||||
# 更新元素
|
||||
arr[1] = 0
|
||||
print("将索引 1 处的元素更新为 0 ,得到 arr =", arr)
|
||||
nums[1] = 0
|
||||
print("\n将索引 1 处的元素更新为 0 ,得到 nums =", nums)
|
||||
|
||||
# 清空列表
|
||||
arr.clear()
|
||||
print("清空列表后 arr =", arr)
|
||||
nums.clear()
|
||||
print("\n清空列表后 nums =", nums)
|
||||
|
||||
# 尾部添加元素
|
||||
arr.append(1)
|
||||
arr.append(3)
|
||||
arr.append(2)
|
||||
arr.append(5)
|
||||
arr.append(4)
|
||||
print("添加元素后 arr =", arr)
|
||||
nums.append(1)
|
||||
nums.append(3)
|
||||
nums.append(2)
|
||||
nums.append(5)
|
||||
nums.append(4)
|
||||
print("\n添加元素后 nums =", nums)
|
||||
|
||||
# 中间插入元素
|
||||
arr.insert(3, 6)
|
||||
print("在索引 3 处插入数字 6 ,得到 arr =", arr)
|
||||
nums.insert(3, 6)
|
||||
print("\n在索引 3 处插入数字 6 ,得到 nums =", nums)
|
||||
|
||||
# 删除元素
|
||||
arr.pop(3)
|
||||
print("删除索引 3 处的元素,得到 arr =", arr)
|
||||
nums.pop(3)
|
||||
print("\n删除索引 3 处的元素,得到 nums =", nums)
|
||||
|
||||
# 通过索引遍历列表
|
||||
count = 0
|
||||
for i in range(len(arr)):
|
||||
count += 1
|
||||
|
||||
# 直接遍历列表元素
|
||||
count = 0
|
||||
for n in arr:
|
||||
count += 1
|
||||
# 遍历列表
|
||||
tmp = []
|
||||
for i in range(len(nums)):
|
||||
tmp.append(nums[i])
|
||||
print(f"\n通过索引遍历列表得到 tmp = {tmp}")
|
||||
|
||||
tmp.clear()
|
||||
for num in nums:
|
||||
tmp.append(num)
|
||||
print(f"\n直接遍历列表元素得到 tmp = {tmp}")
|
||||
|
||||
# 拼接两个列表
|
||||
arr1 = [6, 8, 7, 10, 9]
|
||||
arr += arr1
|
||||
print("将列表 arr1 拼接到 arr 之后,得到 arr =", arr)
|
||||
nums1 = [6, 8, 7, 10, 9]
|
||||
nums += nums1
|
||||
print("\n将列表 nums1 拼接到 nums 之后,得到 nums =", nums)
|
||||
|
||||
# 排序列表
|
||||
arr.sort()
|
||||
print("排序列表后 arr =", arr)
|
||||
nums.sort()
|
||||
print("\n排序列表后 nums =", nums)
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
"""
|
||||
File: my_list.py
|
||||
File: nums.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
"""
|
||||
@ -11,7 +11,7 @@ class MyList:
|
||||
def __init__(self):
|
||||
"""构造方法"""
|
||||
self.__capacity: int = 10 # 列表容量
|
||||
self.__nums: list[int] = [0] * self.__capacity # 数组(存储列表元素)
|
||||
self.__arr: list[int] = [0] * self.__capacity # 数组(存储列表元素)
|
||||
self.__size: int = 0 # 列表长度(即当前元素数量)
|
||||
self.__extend_ratio: int = 2 # 每次列表扩容的倍数
|
||||
|
||||
@ -28,20 +28,20 @@ class MyList:
|
||||
# 索引如果越界则抛出异常,下同
|
||||
if index < 0 or index >= self.__size:
|
||||
raise IndexError("索引越界")
|
||||
return self.__nums[index]
|
||||
return self.__arr[index]
|
||||
|
||||
def set(self, num: int, index: int):
|
||||
"""更新元素"""
|
||||
if index < 0 or index >= self.__size:
|
||||
raise IndexError("索引越界")
|
||||
self.__nums[index] = num
|
||||
self.__arr[index] = num
|
||||
|
||||
def add(self, num: int):
|
||||
"""尾部添加元素"""
|
||||
# 元素数量超出容量时,触发扩容机制
|
||||
if self.size() == self.capacity():
|
||||
self.extend_capacity()
|
||||
self.__nums[self.__size] = num
|
||||
self.__arr[self.__size] = num
|
||||
self.__size += 1
|
||||
|
||||
def insert(self, num: int, index: int):
|
||||
@ -53,8 +53,8 @@ class MyList:
|
||||
self.extend_capacity()
|
||||
# 将索引 index 以及之后的元素都向后移动一位
|
||||
for j in range(self.__size - 1, index - 1, -1):
|
||||
self.__nums[j + 1] = self.__nums[j]
|
||||
self.__nums[index] = num
|
||||
self.__arr[j + 1] = self.__arr[j]
|
||||
self.__arr[index] = num
|
||||
# 更新元素数量
|
||||
self.__size += 1
|
||||
|
||||
@ -62,10 +62,10 @@ class MyList:
|
||||
"""删除元素"""
|
||||
if index < 0 or index >= self.__size:
|
||||
raise IndexError("索引越界")
|
||||
num = self.__nums[index]
|
||||
num = self.__arr[index]
|
||||
# 索引 i 之后的元素都向前移动一位
|
||||
for j in range(index, self.__size - 1):
|
||||
self.__nums[j] = self.__nums[j + 1]
|
||||
self.__arr[j] = self.__arr[j + 1]
|
||||
# 更新元素数量
|
||||
self.__size -= 1
|
||||
# 返回被删除元素
|
||||
@ -74,49 +74,49 @@ class MyList:
|
||||
def extend_capacity(self):
|
||||
"""列表扩容"""
|
||||
# 新建一个长度为原数组 __extend_ratio 倍的新数组,并将原数组拷贝到新数组
|
||||
self.__nums = self.__nums + [0] * self.capacity() * (self.__extend_ratio - 1)
|
||||
self.__arr = self.__arr + [0] * self.capacity() * (self.__extend_ratio - 1)
|
||||
# 更新列表容量
|
||||
self.__capacity = len(self.__nums)
|
||||
self.__capacity = len(self.__arr)
|
||||
|
||||
def to_array(self) -> list[int]:
|
||||
"""返回有效长度的列表"""
|
||||
return self.__nums[: self.__size]
|
||||
return self.__arr[: self.__size]
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化列表
|
||||
my_list = MyList()
|
||||
nums = MyList()
|
||||
# 尾部添加元素
|
||||
my_list.add(1)
|
||||
my_list.add(3)
|
||||
my_list.add(2)
|
||||
my_list.add(5)
|
||||
my_list.add(4)
|
||||
nums.add(1)
|
||||
nums.add(3)
|
||||
nums.add(2)
|
||||
nums.add(5)
|
||||
nums.add(4)
|
||||
print(
|
||||
f"列表 my_list = {my_list.to_array()} ,容量 = {my_list.capacity()} ,长度 = {my_list.size()}"
|
||||
f"列表 nums = {nums.to_array()} ,容量 = {nums.capacity()} ,长度 = {nums.size()}"
|
||||
)
|
||||
|
||||
# 中间插入元素
|
||||
my_list.insert(6, index=3)
|
||||
print("在索引 3 处插入数字 6 ,得到 my_list =", my_list.to_array())
|
||||
nums.insert(6, index=3)
|
||||
print("在索引 3 处插入数字 6 ,得到 nums =", nums.to_array())
|
||||
|
||||
# 删除元素
|
||||
my_list.remove(3)
|
||||
print("删除索引 3 处的元素,得到 my_list =", my_list.to_array())
|
||||
nums.remove(3)
|
||||
print("删除索引 3 处的元素,得到 nums =", nums.to_array())
|
||||
|
||||
# 访问元素
|
||||
num = my_list.get(1)
|
||||
num = nums.get(1)
|
||||
print("访问索引 1 处的元素,得到 num =", num)
|
||||
|
||||
# 更新元素
|
||||
my_list.set(0, 1)
|
||||
print("将索引 1 处的元素更新为 0 ,得到 my_list =", my_list.to_array())
|
||||
nums.set(0, 1)
|
||||
print("将索引 1 处的元素更新为 0 ,得到 nums =", nums.to_array())
|
||||
|
||||
# 测试扩容机制
|
||||
for i in range(10):
|
||||
# 在 i = 5 时,列表长度将超出列表容量,此时触发扩容机制
|
||||
my_list.add(i)
|
||||
nums.add(i)
|
||||
print(
|
||||
f"扩容后的列表 {my_list.to_array()} ,容量 = {my_list.capacity()} ,长度 = {my_list.size()}"
|
||||
f"扩容后的列表 {nums.to_array()} ,容量 = {nums.capacity()} ,长度 = {nums.size()}"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user