Skip to content

Commit f336cca

Browse files
authored
Added doctest to double_hash.py (#11020)
* Added doctest to double_hash.py * Update double_hash.py
1 parent e4eda14 commit f336cca

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

data_structures/hashing/double_hash.py

+33
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,33 @@ def __hash_double_function(self, key, data, increment):
3535
return (increment * self.__hash_function_2(key, data)) % self.size_table
3636

3737
def _collision_resolution(self, key, data=None):
38+
"""
39+
Examples:
40+
41+
1. Try to add three data elements when the size is three
42+
>>> dh = DoubleHash(3)
43+
>>> dh.insert_data(10)
44+
>>> dh.insert_data(20)
45+
>>> dh.insert_data(30)
46+
>>> dh.keys()
47+
{1: 10, 2: 20, 0: 30}
48+
49+
2. Try to add three data elements when the size is two
50+
>>> dh = DoubleHash(2)
51+
>>> dh.insert_data(10)
52+
>>> dh.insert_data(20)
53+
>>> dh.insert_data(30)
54+
>>> dh.keys()
55+
{10: 10, 9: 20, 8: 30}
56+
57+
3. Try to add three data elements when the size is four
58+
>>> dh = DoubleHash(4)
59+
>>> dh.insert_data(10)
60+
>>> dh.insert_data(20)
61+
>>> dh.insert_data(30)
62+
>>> dh.keys()
63+
{9: 20, 10: 10, 8: 30}
64+
"""
3865
i = 1
3966
new_key = self.hash_function(data)
4067

@@ -50,3 +77,9 @@ def _collision_resolution(self, key, data=None):
5077
i += 1
5178

5279
return new_key
80+
81+
82+
if __name__ == "__main__":
83+
import doctest
84+
85+
doctest.testmod()

0 commit comments

Comments
 (0)