Implement an Open Hash Table. Your Hash table will only take numbers as input Yo
ID: 3916642 • Letter: I
Question
Implement an Open Hash Table. Your Hash table will only take numbers as input Your hash function will be hash(val, size-val % size You must implement the following functions Constructor: takes number of rows in column String Method: prints one line for each row in the format shown below hash: Implements the hash function insert: Inserts number into hash table . member: returns True if number is in hash table and false otherwise . delete: removes the number from hash table if it exists You are provided with a test script which will use your Open Hash Table.Explanation / Answer
class OpenHash:
def __init__(self,n):
self.list = []
for i in range(n):
self.list.append(-1)
def __str__(self):
str1 = ""
for i in range(len(self.list)):
str1 = str1 + str(i) + " " + str(self.list[i]) + " "
return str1
def hash(self,i):
return i % len(self.list)
def insert(self,num): #linear probing is used in case of collision
a = self.hash(num)
if self.list[a] == -1:
self.list[a] = num
else:
b = a
a = a + 1
a = a % len(self.list)
while a != b:
if self.list[a] == -1:
self.list[a] = num
break
a = a + 1
a = a % len(self.list)
if a == b:
print("Hash table is full")
def member(self, num):
for i in range(len(self.list)):
if self.list[i] == num:
return True
return False
def delete(self,num):
found = 0
for i in range(len(self.list)):
if self.list[i] == num:
self.list[i] = -1
found = 1
break
if found == 0:
print("Not Found")
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.