mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-05 20:24:19 +00:00
feat: Traditional Chinese version (#1163)
* First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology. * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * Update terminology.md * 操作数量(num. of operations)-> 操作數量 * 字首和->前綴和 * Update figures * 歸 -> 迴 記憶體洩漏 -> 記憶體流失 * Fix the bug of the file filter * 支援 -> 支持 Add zh-Hant/README.md * Add the zh-Hant chapter covers. Bug fixes. * 外掛 -> 擴充功能 * Add the landing page for zh-Hant version * Unify the font of the chapter covers for the zh, en, and zh-Hant version * Move zh-Hant/ to zh-hant/ * Translate terminology.md to traditional Chinese
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
"""
|
||||
File: array.py
|
||||
Created Time: 2022-11-25
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import random
|
||||
|
||||
|
||||
def random_access(nums: list[int]) -> int:
|
||||
"""隨機訪問元素"""
|
||||
# 在區間 [0, len(nums)-1] 中隨機抽取一個數字
|
||||
random_index = random.randint(0, len(nums) - 1)
|
||||
# 獲取並返回隨機元素
|
||||
random_num = nums[random_index]
|
||||
return random_num
|
||||
|
||||
|
||||
# 請注意,Python 的 list 是動態陣列,可以直接擴展
|
||||
# 為了方便學習,本函式將 list 看作長度不可變的陣列
|
||||
def extend(nums: list[int], enlarge: int) -> list[int]:
|
||||
"""擴展陣列長度"""
|
||||
# 初始化一個擴展長度後的陣列
|
||||
res = [0] * (len(nums) + enlarge)
|
||||
# 將原陣列中的所有元素複製到新陣列
|
||||
for i in range(len(nums)):
|
||||
res[i] = nums[i]
|
||||
# 返回擴展後的新陣列
|
||||
return res
|
||||
|
||||
|
||||
def insert(nums: list[int], num: int, index: int):
|
||||
"""在陣列的索引 index 處插入元素 num"""
|
||||
# 把索引 index 以及之後的所有元素向後移動一位
|
||||
for i in range(len(nums) - 1, index, -1):
|
||||
nums[i] = nums[i - 1]
|
||||
# 將 num 賦給 index 處的元素
|
||||
nums[index] = num
|
||||
|
||||
|
||||
def remove(nums: list[int], index: int):
|
||||
"""刪除索引 index 處的元素"""
|
||||
# 把索引 index 之後的所有元素向前移動一位
|
||||
for i in range(index, len(nums) - 1):
|
||||
nums[i] = nums[i + 1]
|
||||
|
||||
|
||||
def traverse(nums: list[int]):
|
||||
"""走訪陣列"""
|
||||
count = 0
|
||||
# 透過索引走訪陣列
|
||||
for i in range(len(nums)):
|
||||
count += nums[i]
|
||||
# 直接走訪陣列元素
|
||||
for num in nums:
|
||||
count += num
|
||||
# 同時走訪資料索引和元素
|
||||
for i, num in enumerate(nums):
|
||||
count += nums[i]
|
||||
count += num
|
||||
|
||||
|
||||
def find(nums: list[int], target: int) -> int:
|
||||
"""在陣列中查詢指定元素"""
|
||||
for i in range(len(nums)):
|
||||
if nums[i] == target:
|
||||
return i
|
||||
return -1
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化陣列
|
||||
arr = [0] * 5
|
||||
print("陣列 arr =", arr)
|
||||
nums = [1, 3, 2, 5, 4]
|
||||
print("陣列 nums =", nums)
|
||||
|
||||
# 隨機訪問
|
||||
random_num: int = random_access(nums)
|
||||
print("在 nums 中獲取隨機元素", random_num)
|
||||
|
||||
# 長度擴展
|
||||
nums: list[int] = extend(nums, 3)
|
||||
print("將陣列長度擴展至 8 ,得到 nums =", nums)
|
||||
|
||||
# 插入元素
|
||||
insert(nums, 6, 3)
|
||||
print("在索引 3 處插入數字 6 ,得到 nums =", nums)
|
||||
|
||||
# 刪除元素
|
||||
remove(nums, 2)
|
||||
print("刪除索引 2 處的元素,得到 nums =", nums)
|
||||
|
||||
# 走訪陣列
|
||||
traverse(nums)
|
||||
|
||||
# 查詢元素
|
||||
index: int = find(nums, 3)
|
||||
print("在 nums 中查詢元素 3 ,得到索引 =", index)
|
||||
@@ -0,0 +1,85 @@
|
||||
"""
|
||||
File: linked_list.py
|
||||
Created Time: 2022-11-25
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
sys.path.append(str(Path(__file__).parent.parent))
|
||||
from modules import ListNode, print_linked_list
|
||||
|
||||
|
||||
def insert(n0: ListNode, P: ListNode):
|
||||
"""在鏈結串列的節點 n0 之後插入節點 P"""
|
||||
n1 = n0.next
|
||||
P.next = n1
|
||||
n0.next = P
|
||||
|
||||
|
||||
def remove(n0: ListNode):
|
||||
"""刪除鏈結串列的節點 n0 之後的首個節點"""
|
||||
if not n0.next:
|
||||
return
|
||||
# n0 -> P -> n1
|
||||
P = n0.next
|
||||
n1 = P.next
|
||||
n0.next = n1
|
||||
|
||||
|
||||
def access(head: ListNode, index: int) -> ListNode | None:
|
||||
"""訪問鏈結串列中索引為 index 的節點"""
|
||||
for _ in range(index):
|
||||
if not head:
|
||||
return None
|
||||
head = head.next
|
||||
return head
|
||||
|
||||
|
||||
def find(head: ListNode, target: int) -> int:
|
||||
"""在鏈結串列中查詢值為 target 的首個節點"""
|
||||
index = 0
|
||||
while head:
|
||||
if head.val == target:
|
||||
return index
|
||||
head = head.next
|
||||
index += 1
|
||||
return -1
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化鏈結串列
|
||||
# 初始化各個節點
|
||||
n0 = ListNode(1)
|
||||
n1 = ListNode(3)
|
||||
n2 = ListNode(2)
|
||||
n3 = ListNode(5)
|
||||
n4 = ListNode(4)
|
||||
# 構建節點之間的引用
|
||||
n0.next = n1
|
||||
n1.next = n2
|
||||
n2.next = n3
|
||||
n3.next = n4
|
||||
print("初始化的鏈結串列為")
|
||||
print_linked_list(n0)
|
||||
|
||||
# 插入節點
|
||||
p = ListNode(0)
|
||||
insert(n0, p)
|
||||
print("插入節點後的鏈結串列為")
|
||||
print_linked_list(n0)
|
||||
|
||||
# 刪除節點
|
||||
remove(n0)
|
||||
print("刪除節點後的鏈結串列為")
|
||||
print_linked_list(n0)
|
||||
|
||||
# 訪問節點
|
||||
node: ListNode = access(n0, 3)
|
||||
print("鏈結串列中索引 3 處的節點的值 = {}".format(node.val))
|
||||
|
||||
# 查詢節點
|
||||
index: int = find(n0, 2)
|
||||
print("鏈結串列中值為 2 的節點的索引 = {}".format(index))
|
||||
@@ -0,0 +1,56 @@
|
||||
"""
|
||||
File: list.py
|
||||
Created Time: 2022-11-25
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化串列
|
||||
nums: list[int] = [1, 3, 2, 5, 4]
|
||||
print("\n串列 nums =", nums)
|
||||
|
||||
# 訪問元素
|
||||
x: int = nums[1]
|
||||
print("\n訪問索引 1 處的元素,得到 x =", x)
|
||||
|
||||
# 更新元素
|
||||
nums[1] = 0
|
||||
print("\n將索引 1 處的元素更新為 0 ,得到 nums =", nums)
|
||||
|
||||
# 清空串列
|
||||
nums.clear()
|
||||
print("\n清空串列後 nums =", nums)
|
||||
|
||||
# 在尾部新增元素
|
||||
nums.append(1)
|
||||
nums.append(3)
|
||||
nums.append(2)
|
||||
nums.append(5)
|
||||
nums.append(4)
|
||||
print("\n新增元素後 nums =", nums)
|
||||
|
||||
# 在中間插入元素
|
||||
nums.insert(3, 6)
|
||||
print("\n在索引 3 處插入數字 6 ,得到 nums =", nums)
|
||||
|
||||
# 刪除元素
|
||||
nums.pop(3)
|
||||
print("\n刪除索引 3 處的元素,得到 nums =", nums)
|
||||
|
||||
# 透過索引走訪串列
|
||||
count = 0
|
||||
for i in range(len(nums)):
|
||||
count += nums[i]
|
||||
# 直接走訪串列元素
|
||||
for num in nums:
|
||||
count += num
|
||||
|
||||
# 拼接兩個串列
|
||||
nums1 = [6, 8, 7, 10, 9]
|
||||
nums += nums1
|
||||
print("\n將串列 nums1 拼接到 nums 之後,得到 nums =", nums)
|
||||
|
||||
# 排序串列
|
||||
nums.sort()
|
||||
print("\n排序串列後 nums =", nums)
|
||||
@@ -0,0 +1,118 @@
|
||||
"""
|
||||
File: my_list.py
|
||||
Created Time: 2022-11-25
|
||||
Author: krahets (krahets@163.com)
|
||||
"""
|
||||
|
||||
|
||||
class MyList:
|
||||
"""串列類別"""
|
||||
|
||||
def __init__(self):
|
||||
"""建構子"""
|
||||
self._capacity: int = 10 # 串列容量
|
||||
self._arr: list[int] = [0] * self._capacity # 陣列(儲存串列元素)
|
||||
self._size: int = 0 # 串列長度(當前元素數量)
|
||||
self._extend_ratio: int = 2 # 每次串列擴容的倍數
|
||||
|
||||
def size(self) -> int:
|
||||
"""獲取串列長度(當前元素數量)"""
|
||||
return self._size
|
||||
|
||||
def capacity(self) -> int:
|
||||
"""獲取串列容量"""
|
||||
return self._capacity
|
||||
|
||||
def get(self, index: int) -> int:
|
||||
"""訪問元素"""
|
||||
# 索引如果越界,則丟擲異常,下同
|
||||
if index < 0 or index >= self._size:
|
||||
raise IndexError("索引越界")
|
||||
return self._arr[index]
|
||||
|
||||
def set(self, num: int, index: int):
|
||||
"""更新元素"""
|
||||
if index < 0 or index >= self._size:
|
||||
raise IndexError("索引越界")
|
||||
self._arr[index] = num
|
||||
|
||||
def add(self, num: int):
|
||||
"""在尾部新增元素"""
|
||||
# 元素數量超出容量時,觸發擴容機制
|
||||
if self.size() == self.capacity():
|
||||
self.extend_capacity()
|
||||
self._arr[self._size] = num
|
||||
self._size += 1
|
||||
|
||||
def insert(self, num: int, index: int):
|
||||
"""在中間插入元素"""
|
||||
if index < 0 or index >= self._size:
|
||||
raise IndexError("索引越界")
|
||||
# 元素數量超出容量時,觸發擴容機制
|
||||
if self._size == self.capacity():
|
||||
self.extend_capacity()
|
||||
# 將索引 index 以及之後的元素都向後移動一位
|
||||
for j in range(self._size - 1, index - 1, -1):
|
||||
self._arr[j + 1] = self._arr[j]
|
||||
self._arr[index] = num
|
||||
# 更新元素數量
|
||||
self._size += 1
|
||||
|
||||
def remove(self, index: int) -> int:
|
||||
"""刪除元素"""
|
||||
if index < 0 or index >= self._size:
|
||||
raise IndexError("索引越界")
|
||||
num = self._arr[index]
|
||||
# 將索引 index 之後的元素都向前移動一位
|
||||
for j in range(index, self._size - 1):
|
||||
self._arr[j] = self._arr[j + 1]
|
||||
# 更新元素數量
|
||||
self._size -= 1
|
||||
# 返回被刪除的元素
|
||||
return num
|
||||
|
||||
def extend_capacity(self):
|
||||
"""串列擴容"""
|
||||
# 新建一個長度為原陣列 _extend_ratio 倍的新陣列,並將原陣列複製到新陣列
|
||||
self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)
|
||||
# 更新串列容量
|
||||
self._capacity = len(self._arr)
|
||||
|
||||
def to_array(self) -> list[int]:
|
||||
"""返回有效長度的串列"""
|
||||
return self._arr[: self._size]
|
||||
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# 初始化串列
|
||||
nums = MyList()
|
||||
# 在尾部新增元素
|
||||
nums.add(1)
|
||||
nums.add(3)
|
||||
nums.add(2)
|
||||
nums.add(5)
|
||||
nums.add(4)
|
||||
print(f"串列 nums = {nums.to_array()} ,容量 = {nums.capacity()} ,長度 = {nums.size()}")
|
||||
|
||||
# 在中間插入元素
|
||||
nums.insert(6, index=3)
|
||||
print("在索引 3 處插入數字 6 ,得到 nums =", nums.to_array())
|
||||
|
||||
# 刪除元素
|
||||
nums.remove(3)
|
||||
print("刪除索引 3 處的元素,得到 nums =", nums.to_array())
|
||||
|
||||
# 訪問元素
|
||||
num = nums.get(1)
|
||||
print("訪問索引 1 處的元素,得到 num =", num)
|
||||
|
||||
# 更新元素
|
||||
nums.set(0, 1)
|
||||
print("將索引 1 處的元素更新為 0 ,得到 nums =", nums.to_array())
|
||||
|
||||
# 測試擴容機制
|
||||
for i in range(10):
|
||||
# 在 i = 5 時,串列長度將超出串列容量,此時觸發擴容機制
|
||||
nums.add(i)
|
||||
print(f"擴容後的串列 {nums.to_array()} ,容量 = {nums.capacity()} ,長度 = {nums.size()}")
|
||||
Reference in New Issue
Block a user