mirror of
https://github.com/jlengrand/Coding4Interviews.git
synced 2026-03-10 08:11:24 +00:00
Starts implementing add method to fill the hash map
This commit is contained in:
@@ -8,14 +8,19 @@ class HashMap():
|
||||
def __init__(self, hash_size=513):
|
||||
self._hash_size = hash_size
|
||||
self._size = 0
|
||||
self.hmap = [] * self._hash_size
|
||||
self.hmap = [0] * self._hash_size
|
||||
|
||||
def add(self, value):
|
||||
"""
|
||||
Adds the provided value to the hashmap
|
||||
Adds the provided value to the hashmap.
|
||||
Raises an Exception if a collision is detected
|
||||
"""
|
||||
key = self._hash(value)
|
||||
self.hmap[key] = value
|
||||
if self.hmap[key] == 0:
|
||||
self.hmap[key] = value
|
||||
self._size += 1
|
||||
else:
|
||||
raise Exception("Collision detected at index %d", key)
|
||||
|
||||
# TODO: Keep implementing
|
||||
|
||||
|
||||
@@ -35,5 +35,11 @@ class test_hash_map(unittest.TestCase):
|
||||
value = ""
|
||||
self.assertRaises(Exception, lambda x : hm._hash(value))
|
||||
|
||||
def test_add(self):
|
||||
hm = HashMap()
|
||||
|
||||
hm.add("a")
|
||||
self.assertEqual(hm.size(), 1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user