From af8014a79ade834227b60159b80e4fffe49604a8 Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Thu, 21 Nov 2013 19:57:32 +0100 Subject: [PATCH] Implements a first get method. This doesnt take collisions into account yet. Also, we use strings to retrieve strings . . . Not really clever --- 02_hash_maps/hm.py | 9 ++++----- 02_hash_maps/hm_test.py | 10 ++++++++++ 2 files changed, 14 insertions(+), 5 deletions(-) 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()