diff --git a/02_hash_maps/hm.py b/02_hash_maps/hm.py index a4ac181..f468556 100644 --- a/02_hash_maps/hm.py +++ b/02_hash_maps/hm.py @@ -27,12 +27,10 @@ class HashMap(): def get(self, value): """ Finds the element in the hash table that may contain the id for - the string ze are looking for + the string we are looking for """ key = self._hash(value) return self.hmap[key] - - def size(self): return self._size @@ -41,12 +39,13 @@ class HashMap(): """ Generates a hash for the given value. The input is expected to be a String, with only ASCII characters. + + # hash function taken from HT3. + # We shift and add : << 4 is a *16 """ if len(value) < 1: raise Exception("Size of value must be greater than one") - # hash function taken from HT3. - # We shift and add : << 4 is a *16 h = 0 for letter in value: h = (h << 4) + ord(letter) diff --git a/02_hash_maps/hm_test.py b/02_hash_maps/hm_test.py index 8743835..150e71a 100644 --- a/02_hash_maps/hm_test.py +++ b/02_hash_maps/hm_test.py @@ -51,6 +51,16 @@ class test_hash_map(unittest.TestCase): value = "" self.assertRaises(Exception, lambda x : hm.get(value)) + value = "One" + hm.add(value) + + hm.add("Two") + hm.add("Three") + hm.add("Four") + + self.assertEqual(hm.size(), 4) + + self.assertEqual(hm.get(value), value) if __name__ == "__main__": unittest.main()