Fix Python code styles.

Update hash_map.
This commit is contained in:
Yudong Jin 2023-02-03 20:03:30 +08:00
parent 15efaca85d
commit a95fe26303
8 changed files with 38 additions and 72 deletions

2
.gitignore vendored
View file

@ -9,5 +9,5 @@ hello-algo.iml
# mkdocs files # mkdocs files
site/ site/
.cache/ .cache/
codes/scripts scripts/
docs/overrides/ docs/overrides/

View file

@ -9,7 +9,7 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import * from include import *
""" 随机访问元素 """ """ 随机访问元素 """
def randomAccess(nums): def random_access(nums):
# 在区间 [0, len(nums)-1] 中随机抽取一个数字 # 在区间 [0, len(nums)-1] 中随机抽取一个数字
random_index = random.randint(0, len(nums) - 1) random_index = random.randint(0, len(nums) - 1)
# 获取并返回随机元素 # 获取并返回随机元素
@ -69,7 +69,7 @@ if __name__ == "__main__":
print("数组 nums =", nums) print("数组 nums =", nums)
""" 随机访问 """ """ 随机访问 """
random_num = randomAccess(nums) random_num = random_access(nums)
print("在 nums 中获取随机元素", random_num) print("在 nums 中获取随机元素", random_num)
""" 长度扩展 """ """ 长度扩展 """

View file

@ -36,10 +36,10 @@ def linear(n):
mapp[i] = str(i) mapp[i] = str(i)
""" 线性阶(递归实现) """ """ 线性阶(递归实现) """
def linearRecur(n): def linear_recur(n):
print("递归 n =", n) print("递归 n =", n)
if n == 1: return if n == 1: return
linearRecur(n - 1) linear_recur(n - 1)
""" 平方阶 """ """ 平方阶 """
def quadratic(n): def quadratic(n):
@ -69,7 +69,7 @@ if __name__ == "__main__":
constant(n) constant(n)
# 线性阶 # 线性阶
linear(n) linear(n)
linearRecur(n) linear_recur(n)
# 平方阶 # 平方阶
quadratic(n) quadratic(n)
quadratic_recur(n) quadratic_recur(n)

View file

@ -6,11 +6,10 @@ Author: msk397 (machangxinq@gmail.com)
""" 键值对 int->String """ """ 键值对 int->String """
class Entry: class Entry:
def __init__(self, key, val): def __init__(self, key: int, val: str):
self.key = key self.key = key
self.val = val self.val = val
""" 基于数组简易实现的哈希表 """ """ 基于数组简易实现的哈希表 """
class ArrayHashMap: class ArrayHashMap:
def __init__(self): def __init__(self):
@ -18,13 +17,13 @@ class ArrayHashMap:
self.bucket = [None] * 100 self.bucket = [None] * 100
""" 哈希函数 """ """ 哈希函数 """
def hashFunc(self, key): def hash_func(self, key):
index = key % 100 index = key % 100
return index return index
""" 查询操作 """ """ 查询操作 """
def get(self, key): def get(self, key):
index = self.hashFunc(key) index = self.hash_func(key)
pair = self.bucket[index] pair = self.bucket[index]
if pair is None: if pair is None:
return None return None
@ -33,17 +32,17 @@ class ArrayHashMap:
""" 添加操作 """ """ 添加操作 """
def put(self, key, val): def put(self, key, val):
pair = Entry(key, val) pair = Entry(key, val)
index = self.hashFunc(key) index = self.hash_func(key)
self.bucket[index] = pair self.bucket[index] = pair
""" 删除操作 """ """ 删除操作 """
def remove(self, key): def remove(self, key):
index = self.hashFunc(key) index = self.hash_func(key)
# 置为None代表删除 # 置为 None ,代表删除
self.bucket[index] = None self.bucket[index] = None
""" 获取所有键值对 """ """ 获取所有键值对 """
def entrySet(self): def entry_set(self):
result = [] result = []
for pair in self.bucket: for pair in self.bucket:
if pair is not None: if pair is not None:
@ -51,7 +50,7 @@ class ArrayHashMap:
return result return result
""" 获取所有键 """ """ 获取所有键 """
def keySet(self): def key_set(self):
result = [] result = []
for pair in self.bucket: for pair in self.bucket:
if pair is not None: if pair is not None:
@ -59,7 +58,7 @@ class ArrayHashMap:
return result return result
""" 获取所有值 """ """ 获取所有值 """
def valueSet(self): def value_set(self):
result = [] result = []
for pair in self.bucket: for pair in self.bucket:
if pair is not None: if pair is not None:
@ -101,13 +100,13 @@ if __name__ == "__main__":
""" 遍历哈希表 """ """ 遍历哈希表 """
print("\n遍历键值对 Key->Value") print("\n遍历键值对 Key->Value")
for pair in mapp.entrySet(): for pair in mapp.entry_set():
print(pair.key, "->", pair.val) print(pair.key, "->", pair.val)
print("\n单独遍历键 Key") print("\n单独遍历键 Key")
for key in mapp.keySet(): for key in mapp.key_set():
print(key) print(key)
print("\n单独遍历值 Value") print("\n单独遍历值 Value")
for val in mapp.valueSet(): for val in mapp.value_set():
print(val) print(val)

View file

@ -35,20 +35,20 @@ class Trunk:
self.prev = prev self.prev = prev
self.str = str self.str = str
def showTrunks(p): def show_trunks(p):
if p is None: if p is None:
return return
showTrunks(p.prev) show_trunks(p.prev)
print(p.str, end='') 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 """Print a binary tree
This tree printer is borrowed from TECHIE DELIGHT This tree printer is borrowed from TECHIE DELIGHT
https://www.techiedelight.com/c-program-print-binary-tree/ https://www.techiedelight.com/c-program-print-binary-tree/
Args: Args:
root ([type]): [description] root ([type]): [description]
prev ([type], optional): [description]. Defaults to None. 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: if root is None:
return return
@ -59,14 +59,14 @@ def print_tree(root, prev=None, isLeft=False):
if prev is None: if prev is None:
trunk.str = '———' trunk.str = '———'
elif isLeft: elif is_left:
trunk.str = '/———' trunk.str = '/———'
prev_str = ' |' prev_str = ' |'
else: else:
trunk.str = '\———' trunk.str = '\———'
prev.str = prev_str prev.str = prev_str
showTrunks(trunk) show_trunks(trunk)
print(' ' + str(root.val)) print(' ' + str(root.val))
if prev: if prev:
prev.str = prev_str prev.str = prev_str

View file

@ -144,7 +144,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
```python title="array.py" ```python title="array.py"
""" 随机访问元素 """ """ 随机访问元素 """
def randomAccess(nums): def random_access(nums):
# 在区间 [0, len(nums)-1] 中随机抽取一个数字 # 在区间 [0, len(nums)-1] 中随机抽取一个数字
random_index = random.randint(0, len(nums) - 1) random_index = random.randint(0, len(nums) - 1)
# 获取并返回随机元素 # 获取并返回随机元素

View file

@ -855,10 +855,10 @@ $$
```python title="space_complexity.py" ```python title="space_complexity.py"
""" 线性阶(递归实现) """ """ 线性阶(递归实现) """
def linearRecur(n): def linear_recur(n):
print("递归 n =", n) print("递归 n =", n)
if n == 1: return if n == 1: return
linearRecur(n - 1) linear_recur(n - 1)
``` ```
=== "Go" === "Go"

View file

@ -514,7 +514,7 @@ $$
/* 删除操作 */ /* 删除操作 */
void remove(int key) { void remove(int key) {
int index = hashFunc(key); int index = hashFunc(key);
// 置为空字符,代表删除 // 置为 nullptr ,代表删除
bucket[index] = nullptr; bucket[index] = nullptr;
} }
}; };
@ -528,36 +528,36 @@ $$
def __init__(self, key, val): def __init__(self, key, val):
self.key = key self.key = key
self.val = val self.val = val
""" 基于数组简易实现的哈希表 """ """ 基于数组简易实现的哈希表 """
class ArrayHashMap: class ArrayHashMap:
def __init__(self): def __init__(self):
# 初始化一个长度为 100 的桶(数组) # 初始化一个长度为 100 的桶(数组)
self.bucket = [None] * 100 self.bucket = [None] * 100
""" 哈希函数 """ """ 哈希函数 """
def hashFunc(self, key): def hash_func(self, key):
index = key % 100 index = key % 100
return index return index
""" 查询操作 """ """ 查询操作 """
def get(self, key): def get(self, key):
index = self.hashFunc(key) index = self.hash_func(key)
pair = self.bucket[index] pair = self.bucket[index]
if pair is None: if pair is None:
return None return None
return pair.val return pair.val
""" 添加操作 """ """ 添加操作 """
def put(self, key, val): def put(self, key, val):
pair = Entry(key, val) pair = Entry(key, val)
index = self.hashFunc(key) index = self.hash_func(key)
self.bucket[index] = pair self.bucket[index] = pair
""" 删除操作 """ """ 删除操作 """
def remove(self, key): def remove(self, key):
index = self.hashFunc(key) index = self.hash_func(key)
# 置为空字符,代表删除 # 置为 None ,代表删除
self.bucket[index] = None self.bucket[index] = None
``` ```
@ -708,39 +708,6 @@ $$
// 置为 null ,代表删除 // 置为 null ,代表删除
this.bucket[index] = 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;
}
} }
``` ```