Added doctest to hash_map.py (#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 <cclauss@me.com>
This commit is contained in:
Suyash Dongre 2023-11-04 17:34:57 +05:30 committed by GitHub
parent 99f3a0e4c9
commit ebfdb127e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -242,6 +242,25 @@ class HashMap(MutableMapping[KEY, VAL]):
self._size_down() self._size_down()
def __getitem__(self, key: KEY) -> VAL: def __getitem__(self, key: KEY) -> VAL:
"""
Returns the item at the given key
>>> hm = HashMap(5)
>>> hm._add_item(1, 10)
>>> hm.__getitem__(1)
10
>>> hm = HashMap(5)
>>> hm._add_item(10, -10)
>>> hm._add_item(20, -20)
>>> hm.__getitem__(20)
-20
>>> hm = HashMap(5)
>>> hm._add_item(-1, 10)
>>> hm.__getitem__(-1)
10
"""
for ind in self._iterate_buckets(key): for ind in self._iterate_buckets(key):
item = self._buckets[ind] item = self._buckets[ind]
if item is None: if item is None:
@ -253,6 +272,20 @@ class HashMap(MutableMapping[KEY, VAL]):
raise KeyError(key) raise KeyError(key)
def __len__(self) -> int: def __len__(self) -> int:
"""
Returns the number of items present in hashmap
>>> hm = HashMap(5)
>>> hm._add_item(1, 10)
>>> hm._add_item(2, 20)
>>> hm._add_item(3, 30)
>>> hm.__len__()
3
>>> hm = HashMap(5)
>>> hm.__len__()
0
"""
return self._len return self._len
def __iter__(self) -> Iterator[KEY]: def __iter__(self) -> Iterator[KEY]: