From a95fe263037a109cab5436822e630894489a9916 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Fri, 3 Feb 2023 20:03:30 +0800 Subject: [PATCH] Fix Python code styles. Update hash_map. --- .gitignore | 2 +- .../chapter_array_and_linkedlist/array.py | 4 +- .../space_complexity.py | 6 +- .../python/chapter_hashing/array_hash_map.py | 25 ++++----- codes/python/include/print_util.py | 12 ++-- docs/chapter_array_and_linkedlist/array.md | 2 +- .../space_complexity.md | 4 +- docs/chapter_hashing/hash_map.md | 55 ++++--------------- 8 files changed, 38 insertions(+), 72 deletions(-) diff --git a/.gitignore b/.gitignore index 8b0e01afd..5cc6522a2 100644 --- a/.gitignore +++ b/.gitignore @@ -9,5 +9,5 @@ hello-algo.iml # mkdocs files site/ .cache/ -codes/scripts +scripts/ docs/overrides/ diff --git a/codes/python/chapter_array_and_linkedlist/array.py b/codes/python/chapter_array_and_linkedlist/array.py index e5979b518..34c2a4c45 100644 --- a/codes/python/chapter_array_and_linkedlist/array.py +++ b/codes/python/chapter_array_and_linkedlist/array.py @@ -9,7 +9,7 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * """ 随机访问元素 """ -def randomAccess(nums): +def random_access(nums): # 在区间 [0, len(nums)-1] 中随机抽取一个数字 random_index = random.randint(0, len(nums) - 1) # 获取并返回随机元素 @@ -69,7 +69,7 @@ if __name__ == "__main__": print("数组 nums =", nums) """ 随机访问 """ - random_num = randomAccess(nums) + random_num = random_access(nums) print("在 nums 中获取随机元素", random_num) """ 长度扩展 """ diff --git a/codes/python/chapter_computational_complexity/space_complexity.py b/codes/python/chapter_computational_complexity/space_complexity.py index 7efe8d948..b4523fedb 100644 --- a/codes/python/chapter_computational_complexity/space_complexity.py +++ b/codes/python/chapter_computational_complexity/space_complexity.py @@ -36,10 +36,10 @@ def linear(n): mapp[i] = str(i) """ 线性阶(递归实现) """ -def linearRecur(n): +def linear_recur(n): print("递归 n =", n) if n == 1: return - linearRecur(n - 1) + linear_recur(n - 1) """ 平方阶 """ def quadratic(n): @@ -69,7 +69,7 @@ if __name__ == "__main__": constant(n) # 线性阶 linear(n) - linearRecur(n) + linear_recur(n) # 平方阶 quadratic(n) quadratic_recur(n) diff --git a/codes/python/chapter_hashing/array_hash_map.py b/codes/python/chapter_hashing/array_hash_map.py index a65f1544b..ce8889924 100644 --- a/codes/python/chapter_hashing/array_hash_map.py +++ b/codes/python/chapter_hashing/array_hash_map.py @@ -6,11 +6,10 @@ Author: msk397 (machangxinq@gmail.com) """ 键值对 int->String """ class Entry: - def __init__(self, key, val): + def __init__(self, key: int, val: str): self.key = key self.val = val - """ 基于数组简易实现的哈希表 """ class ArrayHashMap: def __init__(self): @@ -18,13 +17,13 @@ class ArrayHashMap: self.bucket = [None] * 100 """ 哈希函数 """ - def hashFunc(self, key): + def hash_func(self, key): index = key % 100 return index """ 查询操作 """ def get(self, key): - index = self.hashFunc(key) + index = self.hash_func(key) pair = self.bucket[index] if pair is None: return None @@ -33,17 +32,17 @@ class ArrayHashMap: """ 添加操作 """ def put(self, key, val): pair = Entry(key, val) - index = self.hashFunc(key) + index = self.hash_func(key) self.bucket[index] = pair """ 删除操作 """ def remove(self, key): - index = self.hashFunc(key) - # 置为None,代表删除 + index = self.hash_func(key) + # 置为 None ,代表删除 self.bucket[index] = None """ 获取所有键值对 """ - def entrySet(self): + def entry_set(self): result = [] for pair in self.bucket: if pair is not None: @@ -51,7 +50,7 @@ class ArrayHashMap: return result """ 获取所有键 """ - def keySet(self): + def key_set(self): result = [] for pair in self.bucket: if pair is not None: @@ -59,7 +58,7 @@ class ArrayHashMap: return result """ 获取所有值 """ - def valueSet(self): + def value_set(self): result = [] for pair in self.bucket: if pair is not None: @@ -101,13 +100,13 @@ if __name__ == "__main__": """ 遍历哈希表 """ print("\n遍历键值对 Key->Value") - for pair in mapp.entrySet(): + for pair in mapp.entry_set(): print(pair.key, "->", pair.val) print("\n单独遍历键 Key") - for key in mapp.keySet(): + for key in mapp.key_set(): print(key) print("\n单独遍历值 Value") - for val in mapp.valueSet(): + for val in mapp.value_set(): print(val) diff --git a/codes/python/include/print_util.py b/codes/python/include/print_util.py index 9f211806c..6aa5f7a4e 100644 --- a/codes/python/include/print_util.py +++ b/codes/python/include/print_util.py @@ -35,20 +35,20 @@ class Trunk: self.prev = prev self.str = str -def showTrunks(p): +def show_trunks(p): if p is None: return - showTrunks(p.prev) + show_trunks(p.prev) print(p.str, end='') -def print_tree(root, prev=None, isLeft=False): +def print_tree(root, prev=None, is_left=False): """Print a binary tree This tree printer is borrowed from TECHIE DELIGHT https://www.techiedelight.com/c-program-print-binary-tree/ Args: root ([type]): [description] prev ([type], optional): [description]. Defaults to None. - isLeft (bool, optional): [description]. Defaults to False. + is_left (bool, optional): [description]. Defaults to False. """ if root is None: return @@ -59,14 +59,14 @@ def print_tree(root, prev=None, isLeft=False): if prev is None: trunk.str = '———' - elif isLeft: + elif is_left: trunk.str = '/———' prev_str = ' |' else: trunk.str = '\———' prev.str = prev_str - showTrunks(trunk) + show_trunks(trunk) print(' ' + str(root.val)) if prev: prev.str = prev_str diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index df60c834b..fa4418ee8 100644 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -144,7 +144,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex ```python title="array.py" """ 随机访问元素 """ - def randomAccess(nums): + def random_access(nums): # 在区间 [0, len(nums)-1] 中随机抽取一个数字 random_index = random.randint(0, len(nums) - 1) # 获取并返回随机元素 diff --git a/docs/chapter_computational_complexity/space_complexity.md b/docs/chapter_computational_complexity/space_complexity.md index 9185c1e2f..acb705ca1 100644 --- a/docs/chapter_computational_complexity/space_complexity.md +++ b/docs/chapter_computational_complexity/space_complexity.md @@ -855,10 +855,10 @@ $$ ```python title="space_complexity.py" """ 线性阶(递归实现) """ - def linearRecur(n): + def linear_recur(n): print("递归 n =", n) if n == 1: return - linearRecur(n - 1) + linear_recur(n - 1) ``` === "Go" diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 673727e66..d1cd1a1c1 100644 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -514,7 +514,7 @@ $$ /* 删除操作 */ void remove(int key) { int index = hashFunc(key); - // 置为空字符,代表删除 + // 置为 nullptr ,代表删除 bucket[index] = nullptr; } }; @@ -528,36 +528,36 @@ $$ def __init__(self, key, val): self.key = key self.val = val - + """ 基于数组简易实现的哈希表 """ class ArrayHashMap: def __init__(self): # 初始化一个长度为 100 的桶(数组) self.bucket = [None] * 100 - + """ 哈希函数 """ - def hashFunc(self, key): + def hash_func(self, key): index = key % 100 return index - + """ 查询操作 """ def get(self, key): - index = self.hashFunc(key) + index = self.hash_func(key) pair = self.bucket[index] if pair is None: return None return pair.val - + """ 添加操作 """ def put(self, key, val): pair = Entry(key, val) - index = self.hashFunc(key) + index = self.hash_func(key) self.bucket[index] = pair - + """ 删除操作 """ def remove(self, key): - index = self.hashFunc(key) - # 置为空字符,代表删除 + index = self.hash_func(key) + # 置为 None ,代表删除 self.bucket[index] = None ``` @@ -708,39 +708,6 @@ $$ // 置为 null ,代表删除 this.bucket[index] = null; } - - /* 获取所有键值对 */ - public entries(): (Entry | null)[] { - let arr: (Entry | null)[] = []; - for (let i = 0; i < this.bucket.length; i++) { - if (this.bucket[i]) { - arr.push(this.bucket[i]); - } - } - return arr; - } - - /* 获取所有键 */ - public keys(): (number | undefined)[] { - let arr: (number | undefined)[] = []; - for (let i = 0; i < this.bucket.length; i++) { - if (this.bucket[i]) { - arr.push(this.bucket[i]?.key); - } - } - return arr; - } - - /* 获取所有值 */ - public values(): (string | undefined)[] { - let arr: (string | undefined)[] = []; - for (let i = 0; i < this.bucket.length; i++) { - if (this.bucket[i]) { - arr.push(this.bucket[i]?.val); - } - } - return arr; - } } ```