Skip to content

Commit

Permalink
Fix Python code styles.
Browse files Browse the repository at this point in the history
Update hash_map.
  • Loading branch information
krahets committed Feb 3, 2023
1 parent 15efaca commit a95fe26
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ hello-algo.iml
# mkdocs files
site/
.cache/
codes/scripts
scripts/
docs/overrides/
4 changes: 2 additions & 2 deletions codes/python/chapter_array_and_linkedlist/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from include import *

""" 随机访问元素 """
def randomAccess(nums):
def random_access(nums):
# 在区间 [0, len(nums)-1] 中随机抽取一个数字
random_index = random.randint(0, len(nums) - 1)
# 获取并返回随机元素
Expand Down Expand Up @@ -69,7 +69,7 @@ def find(nums, target):
print("数组 nums =", nums)

""" 随机访问 """
random_num = randomAccess(nums)
random_num = random_access(nums)
print("在 nums 中获取随机元素", random_num)

""" 长度扩展 """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -69,7 +69,7 @@ def build_tree(n):
constant(n)
# 线性阶
linear(n)
linearRecur(n)
linear_recur(n)
# 平方阶
quadratic(n)
quadratic_recur(n)
Expand Down
25 changes: 12 additions & 13 deletions codes/python/chapter_hashing/array_hash_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,24 @@

""" 键值对 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):
# 初始化一个长度为 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
Expand All @@ -33,33 +32,33 @@ def get(self, key):
""" 添加操作 """
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:
result.append(pair)
return result

""" 获取所有键 """
def keySet(self):
def key_set(self):
result = []
for pair in self.bucket:
if pair is not None:
result.append(pair.key)
return result

""" 获取所有值 """
def valueSet(self):
def value_set(self):
result = []
for pair in self.bucket:
if pair is not None:
Expand Down Expand Up @@ -101,13 +100,13 @@ def print(self):

""" 遍历哈希表 """
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)
12 changes: 6 additions & 6 deletions codes/python/include/print_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ def __init__(self, prev=None, str=None):
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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/chapter_array_and_linkedlist/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
# 获取并返回随机元素
Expand Down
4 changes: 2 additions & 2 deletions docs/chapter_computational_complexity/space_complexity.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
55 changes: 11 additions & 44 deletions docs/chapter_hashing/hash_map.md
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ $$
/* 删除操作 */
void remove(int key) {
int index = hashFunc(key);
// 置为空字符,代表删除
// 置为 nullptr ,代表删除
bucket[index] = nullptr;
}
};
Expand All @@ -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
```

Expand Down Expand Up @@ -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;
}
}
```

Expand Down

0 comments on commit a95fe26

Please sign in to comment.