mirror of
https://github.com/jlengrand/Coding4Interviews.git
synced 2026-03-10 08:11:24 +00:00
Modifies hash method to stay within the range of the hash table
This commit is contained in:
@@ -8,6 +8,16 @@ class HashMap():
|
||||
def __init__(self, hash_size=513):
|
||||
self._hash_size = hash_size
|
||||
self._size = 0
|
||||
self.hmap = [] * self._hash_size
|
||||
|
||||
def add(self, value):
|
||||
"""
|
||||
Adds the provided value to the hashmap
|
||||
"""
|
||||
key = self._hash(value)
|
||||
self.hmap[key] = value
|
||||
|
||||
# TODO: Keep implementing
|
||||
|
||||
def size(self):
|
||||
return self._size
|
||||
@@ -26,6 +36,6 @@ class HashMap():
|
||||
for letter in value:
|
||||
h = (h << 4) + ord(letter)
|
||||
|
||||
return h
|
||||
return h % self._hash_size
|
||||
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ class test_hash_map(unittest.TestCase):
|
||||
self.assertEqual(hm._hash(value), ord(value))
|
||||
|
||||
value = "test"
|
||||
self.assertEqual(hm._hash(value), 502948)
|
||||
self.assertEqual(hm._hash(value), 208)
|
||||
|
||||
value = ""
|
||||
self.assertRaises(Exception, lambda x : hm._hash(value))
|
||||
|
||||
Reference in New Issue
Block a user