diff --git a/02_hash_maps/hm.py b/02_hash_maps/hm.py index f807ad8..e7fba61 100644 --- a/02_hash_maps/hm.py +++ b/02_hash_maps/hm.py @@ -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 diff --git a/02_hash_maps/hm_test.py b/02_hash_maps/hm_test.py index 90c38de..2649bfe 100644 --- a/02_hash_maps/hm_test.py +++ b/02_hash_maps/hm_test.py @@ -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()