MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / __getitem__

Method __getitem__

data_structures/hashing/hash_map.py:267–295  ·  view source on GitHub ↗

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

(self, key: KEY)

Source from the content-addressed store, hash-verified

265 self._size_down()
266
267 def __getitem__(self, key: KEY) -> VAL:
268 """
269 Returns the item at the given key
270
271 >>> hm = HashMap(5)
272 >>> hm._add_item(1, 10)
273 >>> hm.__getitem__(1)
274 10
275
276 >>> hm = HashMap(5)
277 >>> hm._add_item(10, -10)
278 >>> hm._add_item(20, -20)
279 >>> hm.__getitem__(20)
280 -20
281
282 >>> hm = HashMap(5)
283 >>> hm._add_item(-1, 10)
284 >>> hm.__getitem__(-1)
285 10
286 """
287 for ind in self._iterate_buckets(key):
288 item = self._buckets[ind]
289 if item is None:
290 break
291 if item is _deleted:
292 continue
293 if item.key == key:
294 return item.val
295 raise KeyError(key)
296
297 def __len__(self) -> int:
298 """

Callers

nothing calls this directly

Calls 1

_iterate_bucketsMethod · 0.95

Tested by

no test coverage detected