Skip to content

Commit ebfdb12

Browse files
Suyashd999pre-commit-ci[bot]cclauss
authored
Added doctest to hash_map.py (TheAlgorithms#11105)
* Added doctest to heap.py * Added doctest to hash_map.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update hash_map.py * Added doctest to hash_map.py * Added doctest to hash_map.py * Added doctest to detecting_english_programmatically.py * Update detecting_english_programmatically.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 99f3a0e commit ebfdb12

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Diff for: data_structures/hashing/hash_map.py

+33
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,25 @@ def __delitem__(self, key: KEY) -> None:
242242
self._size_down()
243243

244244
def __getitem__(self, key: KEY) -> VAL:
245+
"""
246+
Returns the item at the given key
247+
248+
>>> hm = HashMap(5)
249+
>>> hm._add_item(1, 10)
250+
>>> hm.__getitem__(1)
251+
10
252+
253+
>>> hm = HashMap(5)
254+
>>> hm._add_item(10, -10)
255+
>>> hm._add_item(20, -20)
256+
>>> hm.__getitem__(20)
257+
-20
258+
259+
>>> hm = HashMap(5)
260+
>>> hm._add_item(-1, 10)
261+
>>> hm.__getitem__(-1)
262+
10
263+
"""
245264
for ind in self._iterate_buckets(key):
246265
item = self._buckets[ind]
247266
if item is None:
@@ -253,6 +272,20 @@ def __getitem__(self, key: KEY) -> VAL:
253272
raise KeyError(key)
254273

255274
def __len__(self) -> int:
275+
"""
276+
Returns the number of items present in hashmap
277+
278+
>>> hm = HashMap(5)
279+
>>> hm._add_item(1, 10)
280+
>>> hm._add_item(2, 20)
281+
>>> hm._add_item(3, 30)
282+
>>> hm.__len__()
283+
3
284+
285+
>>> hm = HashMap(5)
286+
>>> hm.__len__()
287+
0
288+
"""
256289
return self._len
257290

258291
def __iter__(self) -> Iterator[KEY]:

0 commit comments

Comments
 (0)