Question: ****python**** class HashTable: def __init__(self, s=13): self.size = s self.slots = [None] * self.size def put(self,key,data): hashvalue = self.hashfunction(key,len(self.slots)) ... def hashfunction(self,key,size): ... def __str__(self):
****python**** class HashTable: def __init__(self, s=13): self.size = s self.slots = [None] * self.size def put(self,key,data): hashvalue = self.hashfunction(key,len(self.slots)) ... def hashfunction(self,key,size): ... def __str__(self): return str(self.slots)
Rewrite/Simplify the above Hashtable class. We will take a string and insert it into a list representing the hashtable using a hash function for Strings. In order to use a better hash function, we will need to calculate the weighted sum of all characters in the string. You can use the following function to get the ACSII code in a letter
value = ord('a') You may assume no collisions will occur in this question and there will be space for the value.
The 'None' value will be used to represent empty positions in the hashtable.
For example:
| Test | Result |
|---|---|
my_hash_table=HashTable(7) my_hash_table.put('hello') my_hash_table.put('welldone') my_hash_table.put('abc') print(my_hash_table) | ['hello', None, 'abc', None, None, None, 'welldone'] |
Template for the answer is:
class HashTable:
def __init__(self, s=13):
self.size = s
self.slots = [None] * self.size
def put(self,key):
# two lines only
def hashfunction(self,key,size):
def __str__(self):
return str(self.slots)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
