Skip to content

Commit 44518ad

Browse files
committed
fix hash
1 parent e219af9 commit 44518ad

File tree

4 files changed

+14
-14
lines changed

4 files changed

+14
-14
lines changed

docs/7_哈希表/hashtable.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ def _find_slot(self, key, for_insert=False):
7575
if not for_insert: # 查找是否存在 key
7676
while self._table[index] is not HashTable.UNUSED:
7777
if self._table[index] is HashTable.EMPTY:
78-
index = (index + hash_times * hash_times) % _len # 一个简单的二次方探查
78+
index = (base_index + hash_times * hash_times) % _len # 一个简单的二次方探查
7979
continue
8080
elif self._table[index].key == key:
8181
return index
82-
index = (index + hash_times * hash_times) % _len
82+
index = (base_index + hash_times * hash_times) % _len
8383
hash_times += 1
8484
return None
8585
else:
8686
while not self._slot_can_insert(index): # 循环直到找到一个可以插入的槽
87-
index = (index + hash_times * hash_times) % _len
87+
index = (base_index + hash_times * hash_times) % _len
8888
hash_times += 1
8989
return index
9090

docs/8_字典/dict_adt.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ def _find_slot(self, key, for_insert=False):
7575
if not for_insert: # 查找是否存在 key
7676
while self._table[index] is not HashTable.UNUSED:
7777
if self._table[index] is HashTable.EMPTY:
78-
index = (index + hash_times * hash_times) % _len # 一个简单的二次方探查
78+
index = (base_index + hash_times * hash_times) % _len # 一个简单的二次方探查
7979
continue
8080
elif self._table[index].key == key:
8181
return index
82-
index = (index + hash_times * hash_times) % _len
82+
index = (base_index + hash_times * hash_times) % _len
8383
hash_times += 1
8484
return None
8585
else:
8686
while not self._slot_can_insert(index): # 循环直到找到一个可以插入的槽
87-
index = (index + hash_times * hash_times) % _len
87+
index = (base_index + hash_times * hash_times) % _len
8888
hash_times += 1
8989
return index
9090

docs/9_集合/set.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class SetADT(HashTable):
3535
return super(SetADT, self).add(key, True)
3636
```
3737

38-
当然其它数学上的操作就麻烦点了。
38+
当然其它数学上的操作就麻烦点了,不过也很容易实现
3939

4040

4141
# 思考题
@@ -44,4 +44,4 @@ class SetADT(HashTable):
4444

4545

4646
# 延伸阅读
47-
阅读 python 文档关于 set 的相关章节,了解 set 还有哪些操作?比如比较运算符的概念
47+
阅读 python 文档关于 set 的相关章节,了解 set 还有哪些操作?比如比较运算符的概念,比较两个集合意味着什么。

docs/9_集合/set_adt.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ def _find_slot(self, key, for_insert=False):
7575
if not for_insert: # 查找是否存在 key
7676
while self._table[index] is not HashTable.UNUSED:
7777
if self._table[index] is HashTable.EMPTY:
78-
index = (index + hash_times * hash_times) % _len # 一个简单的二次方探查
78+
index = (base_index + hash_times * hash_times) % _len # 一个简单的二次方探查
7979
continue
8080
elif self._table[index].key == key:
8181
return index
82-
index = (index + hash_times * hash_times) % _len
82+
index = (base_index + hash_times * hash_times) % _len
8383
hash_times += 1
8484
return None
8585
else:
8686
while not self._slot_can_insert(index): # 循环直到找到一个可以插入的槽
87-
index = (index + hash_times * hash_times) % _len
87+
index = (base_index + hash_times * hash_times) % _len
8888
hash_times += 1
8989
return index
9090

@@ -194,9 +194,9 @@ def test_set_adt():
194194
sb.add(4)
195195
sb.add(5)
196196

197-
sorted(list(sa & sb)) == [3]
198-
sorted(list(sa - sb)) == [1, 2]
199-
sorted(list(sa | sb)) == [1, 2, 3, 4, 5]
197+
assert sorted(list(sa & sb)) == [3]
198+
assert sorted(list(sa - sb)) == [1, 2]
199+
assert sorted(list(sa | sb)) == [1, 2, 3, 4, 5]
200200

201201

202202
if __name__ == '__main__':

0 commit comments

Comments
 (0)