Skip to content

Commit 5598625

Browse files
committed
Added doctest to hash_map.py
1 parent d86ce4f commit 5598625

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

Diff for: data_structures/hashing/hash_map.py

+126
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,26 @@ def _get_next_ind(self, ind: int) -> int:
5454
Get next index.
5555
5656
Implements linear open addressing.
57+
58+
Example 1:
59+
>>> hm = HashMap(5)
60+
>>> hm._get_next_ind(3)
61+
4
62+
63+
Example 2:
64+
>>> hm = HashMap(5)
65+
>>> hm._get_next_ind(5)
66+
1
67+
68+
Example 3:
69+
>>> hm = HashMap(5)
70+
>>> hm._get_next_ind(6)
71+
2
72+
73+
Example 4:
74+
>>> hm = HashMap(5)
75+
>>> hm._get_next_ind(9)
76+
0
5777
"""
5878
return (ind + 1) % len(self._buckets)
5979

@@ -82,6 +102,16 @@ def _is_full(self) -> bool:
82102
Return true if we have reached safe capacity.
83103
84104
So we need to increase the number of buckets to avoid collisions.
105+
106+
>>> hm = HashMap(2)
107+
>>> hm._add_item(1,10)
108+
>>> hm._add_item(2,20)
109+
>>> hm._is_full()
110+
True
111+
112+
>>> hm = HashMap(2)
113+
>>> hm._is_full()
114+
False
85115
"""
86116
limit = len(self._buckets) * self._capacity_factor
87117
return len(self) >= int(limit)
@@ -114,17 +144,107 @@ def _iterate_buckets(self, key: KEY) -> Iterator[int]:
114144
ind = self._get_next_ind(ind)
115145

116146
def _add_item(self, key: KEY, val: VAL) -> None:
147+
"""
148+
1. Trying to add 3 elements when size is 5
149+
>>> hm = HashMap(5)
150+
>>> hm._add_item(1,10)
151+
>>> hm._add_item(2,20)
152+
>>> hm._add_item(3,30)
153+
>>> hm
154+
HashMap(1: 10 ,2: 20 ,3: 30)
155+
156+
2. Trying to add 3 elements when size is 5
157+
>>> hm = HashMap(5)
158+
>>> hm._add_item(-5,10)
159+
>>> hm._add_item(6,30)
160+
>>> hm._add_item(-7,20)
161+
>>> hm
162+
HashMap(-5: 10 ,6: 30 ,-7: 20)
163+
164+
3. Trying to add 3 elements when size is 1
165+
>>> hm = HashMap(1)
166+
>>> hm._add_item(10,13.2)
167+
>>> hm._add_item(6,5.26)
168+
>>> hm._add_item(7,5.155)
169+
>>> hm
170+
HashMap(10: 13.2)
171+
172+
4. Trying to add element with key in floating value
173+
>>> hm = HashMap(5)
174+
>>> hm._add_item(1.5,10)
175+
>>> hm
176+
HashMap(1.5: 10)
177+
178+
5. Trying to add item with same key
179+
>>> hm = HashMap(5)
180+
>>> hm._add_item(1,10)
181+
>>> hm._add_item(1,20)
182+
>>> hm
183+
HashMap(1: 20)
184+
"""
117185
for ind in self._iterate_buckets(key):
118186
if self._try_set(ind, key, val):
119187
break
120188

121189
def __setitem__(self, key: KEY, val: VAL) -> None:
190+
"""
191+
1. Changing value of item whose key is present
192+
>>> hm = HashMap(5)
193+
>>> hm._add_item(1,10)
194+
>>> hm.__setitem__(1,20)
195+
>>> hm
196+
HashMap(1: 20)
197+
198+
2. Changing value of item whose key is not present
199+
>>> hm = HashMap(5)
200+
>>> hm._add_item(1,10)
201+
>>> hm.__setitem__(0,20)
202+
>>> hm
203+
HashMap(0: 20 ,1: 10)
204+
205+
3. Changing value of same item multiple times
206+
>>> hm = HashMap(5)
207+
>>> hm._add_item(1,10)
208+
>>> hm.__setitem__(1,20)
209+
>>> hm.__setitem__(1,30)
210+
>>> hm
211+
HashMap(1: 30)
212+
"""
122213
if self._is_full():
123214
self._size_up()
124215

125216
self._add_item(key, val)
126217

127218
def __delitem__(self, key: KEY) -> None:
219+
"""
220+
Example 1.
221+
>>> hm = HashMap(5)
222+
>>> hm._add_item(1,10)
223+
>>> hm._add_item(2,20)
224+
>>> hm._add_item(3,30)
225+
>>> hm.__delitem__(3)
226+
>>> hm
227+
HashMap(1: 10 ,2: 20)
228+
229+
Example 2.
230+
>>> hm = HashMap(5)
231+
>>> hm._add_item(-5,10)
232+
>>> hm._add_item(6,30)
233+
>>> hm._add_item(-7,20)
234+
>>> hm.__delitem__(-5)
235+
>>> hm
236+
HashMap(6: 30 ,-7: 20)
237+
238+
Example 3: Trying to remove non-existing item
239+
>>> hm = HashMap(5)
240+
>>> hm._add_item(1,10)
241+
>>> hm._add_item(2,20)
242+
>>> hm._add_item(3,30)
243+
>>> hm.__delitem__(4)
244+
Traceback (most recent call last):
245+
...
246+
KeyError: 4
247+
"""
128248
for ind in self._iterate_buckets(key):
129249
item = self._buckets[ind]
130250
if item is None:
@@ -160,3 +280,9 @@ def __repr__(self) -> str:
160280
f"{item.key}: {item.val}" for item in self._buckets if item
161281
)
162282
return f"HashMap({val_string})"
283+
284+
if __name__ == "__main__":
285+
286+
import doctest
287+
288+
doctest.testmod()

0 commit comments

Comments
 (0)