Skip to content

Commit 29b8ccd

Browse files
authored
Added doctest to hash_table.py (TheAlgorithms#10984)
1 parent 42c49ee commit 29b8ccd

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

data_structures/hashing/hash_table.py

+81
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,29 @@ def __init__(
2121
self._keys: dict = {}
2222

2323
def keys(self):
24+
"""
25+
The keys function returns a dictionary containing the key value pairs.
26+
key being the index number in hash table and value being the data value.
27+
28+
Examples:
29+
1. creating HashTable with size 10 and inserting 3 elements
30+
>>> ht = HashTable(10)
31+
>>> ht.insert_data(10)
32+
>>> ht.insert_data(20)
33+
>>> ht.insert_data(30)
34+
>>> ht.keys()
35+
{0: 10, 1: 20, 2: 30}
36+
37+
2. creating HashTable with size 5 and inserting 5 elements
38+
>>> ht = HashTable(5)
39+
>>> ht.insert_data(5)
40+
>>> ht.insert_data(4)
41+
>>> ht.insert_data(3)
42+
>>> ht.insert_data(2)
43+
>>> ht.insert_data(1)
44+
>>> ht.keys()
45+
{0: 5, 4: 4, 3: 3, 2: 2, 1: 1}
46+
"""
2447
return self._keys
2548

2649
def balanced_factor(self):
@@ -37,6 +60,43 @@ def _step_by_step(self, step_ord):
3760
print(self.values)
3861

3962
def bulk_insert(self, values):
63+
"""
64+
bulk_insert is used for entering more than one element at a time
65+
in the HashTable.
66+
67+
Examples:
68+
1.
69+
>>> ht = HashTable(5)
70+
>>> ht.bulk_insert((10,20,30))
71+
step 1
72+
[0, 1, 2, 3, 4]
73+
[10, None, None, None, None]
74+
step 2
75+
[0, 1, 2, 3, 4]
76+
[10, 20, None, None, None]
77+
step 3
78+
[0, 1, 2, 3, 4]
79+
[10, 20, 30, None, None]
80+
81+
2.
82+
>>> ht = HashTable(5)
83+
>>> ht.bulk_insert([5,4,3,2,1])
84+
step 1
85+
[0, 1, 2, 3, 4]
86+
[5, None, None, None, None]
87+
step 2
88+
[0, 1, 2, 3, 4]
89+
[5, None, None, None, 4]
90+
step 3
91+
[0, 1, 2, 3, 4]
92+
[5, None, None, 3, 4]
93+
step 4
94+
[0, 1, 2, 3, 4]
95+
[5, None, 2, 3, 4]
96+
step 5
97+
[0, 1, 2, 3, 4]
98+
[5, 1, 2, 3, 4]
99+
"""
40100
i = 1
41101
self.__aux_list = values
42102
for value in values:
@@ -69,6 +129,21 @@ def rehashing(self):
69129
self.insert_data(value)
70130

71131
def insert_data(self, data):
132+
"""
133+
insert_data is used for inserting a single element at a time in the HashTable.
134+
135+
Examples:
136+
137+
>>> ht = HashTable(3)
138+
>>> ht.insert_data(5)
139+
>>> ht.keys()
140+
{2: 5}
141+
>>> ht = HashTable(5)
142+
>>> ht.insert_data(30)
143+
>>> ht.insert_data(50)
144+
>>> ht.keys()
145+
{0: 30, 1: 50}
146+
"""
72147
key = self.hash_function(data)
73148

74149
if self.values[key] is None:
@@ -84,3 +159,9 @@ def insert_data(self, data):
84159
else:
85160
self.rehashing()
86161
self.insert_data(data)
162+
163+
164+
if __name__ == "__main__":
165+
import doctest
166+
167+
doctest.testmod()

0 commit comments

Comments
 (0)