mirror of
https://github.com/jlengrand/Coding4Interviews.git
synced 2026-03-10 08:11:24 +00:00
Finishes implementing the bare simple HM ersion using items. Now going to modify the version handling collisions
This commit is contained in:
@@ -145,9 +145,12 @@ class HashMapWithItem():
|
||||
Adds the provided value to the hashmap.
|
||||
Raises an Exception if a collision is detected
|
||||
"""
|
||||
# item to be saved in the HM
|
||||
item = _HashMapItem(key, value)
|
||||
|
||||
my_key = self._hash(key)
|
||||
if self.hmap[my_key] == None:
|
||||
self.hmap[my_key] = value
|
||||
self.hmap[my_key] = item
|
||||
self._size += 1
|
||||
else:
|
||||
raise Exception("Collision detected at index %d", key)
|
||||
@@ -158,7 +161,7 @@ class HashMapWithItem():
|
||||
the string we are looking for
|
||||
"""
|
||||
my_key = self._hash(key)
|
||||
return self.hmap[my_key]
|
||||
return self.hmap[my_key].v # returns the value
|
||||
|
||||
def size(self):
|
||||
return self._size
|
||||
|
||||
@@ -80,7 +80,6 @@ class test_hash_map_table_collision(unittest.TestCase):
|
||||
self.assertEqual(hm.get("Five"), None)
|
||||
self.assertEqual(hm.get(key), [value, value3])
|
||||
|
||||
|
||||
class test_hash_map_with_item(unittest.TestCase):
|
||||
|
||||
def test_hash_size(self):
|
||||
|
||||
Reference in New Issue
Block a user