From b7364b40daa0c8b8d7c30366ab86d6dca01ff815 Mon Sep 17 00:00:00 2001 From: julien lengrand-lambert Date: Tue, 26 Nov 2013 19:10:08 +0100 Subject: [PATCH] Corrects the implementation of add. It is not possible any more to create use twice the same key. --- 02_hash_maps/hm.py | 3 +++ 02_hash_maps/hm_test.py | 10 ++++------ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/02_hash_maps/hm.py b/02_hash_maps/hm.py index 7c31082..d44d952 100644 --- a/02_hash_maps/hm.py +++ b/02_hash_maps/hm.py @@ -149,6 +149,9 @@ class HMTableCollision(HashMap): if self.hmap[my_key] == None: self.hmap[my_key] = [item] else: + # we check if same key already exists + if [key in item.k for item in self.hmap[my_key]]: + raise Exception("This key already exists!") self.hmap[my_key].append(item) self._size += 1 diff --git a/02_hash_maps/hm_test.py b/02_hash_maps/hm_test.py index 9f338d0..aebb12a 100644 --- a/02_hash_maps/hm_test.py +++ b/02_hash_maps/hm_test.py @@ -51,7 +51,9 @@ class test_hash_map_table_collision(unittest.TestCase): hm.add("a", "Ibiza") self.assertEqual(hm.size(), 1) - hm.add("a", "Ibiza2") + self.assertRaises(Exception, lambda x : hm.add("a", "Ibiza")) + + hm.add("a2", "Ibiza2") self.assertEqual(hm.size(), 2) # TODO: Should return exception. Not allow twice the same key! def test_get(self): @@ -70,11 +72,7 @@ class test_hash_map_table_collision(unittest.TestCase): hm.add("Three", "Berlin") hm.add("Four", "Chicago") - value3 = "Ibiza2" - hm.add(key, value3) - - - self.assertEqual(hm.size(), 5) + self.assertEqual(hm.size(), 4) self.assertEqual(hm.get(key2), value2) self.assertEqual(hm.get("Five"), None)