mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 03:56:28 +08:00
Fix Python code styles.
Update hash_map.
This commit is contained in:
parent
15efaca85d
commit
a95fe26303
8 changed files with 38 additions and 72 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -9,5 +9,5 @@ hello-algo.iml
|
|||
# mkdocs files
|
||||
site/
|
||||
.cache/
|
||||
codes/scripts
|
||||
scripts/
|
||||
docs/overrides/
|
||||
|
|
|
@ -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)
|
||||
|
||||
""" 长度扩展 """
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
# 获取并返回随机元素
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in a new issue