Hash Table with Chaining
Create a hash table with chaining collision resolution and upload your code here! Your hash table should be an implementation of the following hash table ADT:
Hash Table ADT:
Hashtable(): creates a hash table object
get(key) : returns the value corresponding to the key if the key is present in the hash table
get_size() : return the size of the hash table
add(key, value) : adds new valid key, value pair to the hash table, if already present updates the value
remove(key) : removes the key, value pair
is_empty() : returns true if size is zero
NOTE: You need to use a nested list, do not use a dictionary in implementing your hash table! The size of hash table should be small around 10, and the hash function should produce collisions in order to check the functionality of your hash table. For example, the main instance variable can be self.table = [[], [], [], [], []], a nested list of size 5. You can add items to it in form of a tuple (key, value). You need to search items by keys using a linear search (on sorted inner lists). So, in order to retrieve an item by its key (for example goat: 0), you need to hash the key into an index (hash of goat may return index 3), retrieve the inner list at this index, and perform a linear search on the sorted list to get the item (or its value).
Your code should work with the following tester:
data = ['goat', 'pig', 'chicken', 'dog', 'lion', 'tiger', 'cow', 'cat'] # make a hash table with key-value pairs: 'goat': 0, 'pig': 1, 'chicken': 2, etc. h = Hashtable() for i in range(len(data)): h.add(data[i], i) # the key is data[i], the value is i # print the hash table items for key in data: print(h.get(key)) # test the method get() and if items in the hash table are correct for i in range(len(data)): assert h.get(data[i]) == i # test the method get_size() n = h.get_size() assert n == 8 # test the method remove() and is_empty() for i in data: h.remove(i) print(h.is_empty()) assert h.is_empty() Upload your file hashtable.py here!