From 7f8736c9ca63855164077c5a02d4b0b8403ba393 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Thu, 21 Nov 2013 19:54:16 +0100 Subject: [PATCH] Starts implementing get method of the HashMap --- 02_hash_maps/hm.py | 14 ++++++++++++-- 02_hash_maps/hm_test.py | 6 ++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/02_hash_maps/hm.py b/02_hash_maps/hm.py index e7fba61..a4ac181 100644 --- a/02_hash_maps/hm.py +++ b/02_hash_maps/hm.py @@ -8,7 +8,7 @@ class HashMap(): def __init__(self, hash_size=513): self._hash_size = hash_size self._size = 0 - self.hmap = [0] * self._hash_size + self.hmap = [None] * self._hash_size def add(self, value): """ @@ -16,7 +16,7 @@ class HashMap(): Raises an Exception if a collision is detected """ key = self._hash(value) - if self.hmap[key] == 0: + if self.hmap[key] == None: self.hmap[key] = value self._size += 1 else: @@ -24,6 +24,16 @@ class HashMap(): # TODO: Keep implementing + def get(self, value): + """ + Finds the element in the hash table that may contain the id for + the string ze are looking for + """ + key = self._hash(value) + return self.hmap[key] + + + def size(self): return self._size diff --git a/02_hash_maps/hm_test.py b/02_hash_maps/hm_test.py index 5e088ff..8743835 100644 --- a/02_hash_maps/hm_test.py +++ b/02_hash_maps/hm_test.py @@ -45,6 +45,12 @@ class test_hash_map(unittest.TestCase): self.assertRaises(Exception, lambda x : hm.add(a)) self.assertEqual(hm.size(), 1) + def test_get(self): + + hm = HashMap() + value = "" + self.assertRaises(Exception, lambda x : hm.get(value)) + if __name__ == "__main__": unittest.main()