mirror of
https://github.com/jlengrand/Coding4Interviews.git
synced 2026-03-10 08:11:24 +00:00
Starts implementing get method of the HashMap
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user