Problem 3 Hashing The HashTable class implemented in chapter 5 of your online textbook exhibits undesirable behavior if you use put(key,val) to add a new key-value pair when the table is full. Re-implement the put method so that the table will automatically increase in size when the method detects that the table is full. The new size should be a prime number that approximately twice the current size of the table. For example, if the original size is 11, the new size would be 23. We are providing you with a good sequence of hash table sizes up to a maximum size of 1543. You may assume that maximum size you will need to implement for the table is 1543. good_sizes = [11, 23, 53, 97, 193, 769, 1543]
Problem 3 Part 2 Increasing the table size (30 points)
After empty_slots() is implemented, you will use it to test if the
table is full before a value is inserted into the table. If the
table is full you will create a new table using the next value in
the good_sizes list the and re-insert each key from the previous
table into the new table. You may want to create a helper method
for this, but it’s not necessar
HashTable.py
foundPrimes = [2, 3]
def findNextPrime():
check = foundPrimes[-1] + 2
def isPrime(n):
for prime in
foundPrimes:
if n % prime == 0:
return False
return True
while not isPrime(check):
check += 2
foundPrimes.append(check)
good_sizes = [11, 23, 53, 97, 193, 389, 769, 1543]
class HashTable:
def __init__(self):
self.size = 11
self.slots = [None] *
self.size
self.data = [None] *
self.size
self.emptyslots =
self.size
#
https://planetmath.org/goodhashtableprimes
self.good_sizes =
good_sizes
def getNextGoodSize(self):
# Find the first valid
good size
for size in
self.good_sizes:
if size > self.size:
return size
# Otherwise, find the
first prime number that is at least twice the current size
# and add that value to
the list of known good sizes.
size =
foundPrimes[-1]
while size <
self.size * 2:
findNextPrime()
size = foundPrimes[-1]
good_sizes.append(size)
return size
def hashfunction(self, key, size):
return key % size
def rehash(self, oldhash, size):
return (oldhash + 1) %
size
def get(self, key):
startslot =
self.hashfunction(key, len(self.slots))
data = None
stop = False
found = False
position =
startslot
while
self.slots[position] != None and not found and not stop:
if self.slots[position] == key:
found = True
data = self.data[position]
else:
position = self.rehash(position, len(self.slots))
if position == startslot:
stop = True
return data
def __getitem__(self, key):
return self.get(key)
def __setitem__(self, key, data):
self.put(key, data)
# Problem 3 Part 1
def empty_slots(self):
return
self.emptyslots
# Problem 3 Part 2
def put(self, key, data):
if self.empty_slots() ==
0:
# Rehash
newSize = self.getNextGoodSize()
oldSlots = self.slots
oldData = self.data
self.size = newSize
self.slots = [None] * self.size
self.data = [None] * self.size
self.emptyslots = self.size
for index in range(0, len(oldSlots)):
self.put(oldSlots[index], oldData[index])
# This code has been
condensed from its original implementation
# to be easier to
read
hashvalue =
self.hashfunction(key, len(self.slots))
while
self.slots[hashvalue] != None and self.slots[hashvalue] !=
key:
hashvalue = self.rehash(hashvalue, len(self.slots))
if
self.slots[hashvalue] == None:
self.slots[hashvalue] = key
self.data[hashvalue] = data
self.emptyslots -= 1
else:
self.data[hashvalue] = data #replace
Problem 3 Hashing The HashTable class implemented in chapter 5 of your online textbook exhibits undesirable behavior if you use put(key,val) to add a new key-value pair when the table is full. Re-imp...
PROGRAM DESCRIPTION Implement a hash table class. Your hash table should resolve collisions by chaining and use the multiplication method to generate hash keys. You may use your own linked list code from a previous assignment or you can use a standard sequence container. Partial definitions for the hash table class and a generic data class are provided (C++ and Java versions). You may use them as a starting point if you wish. If you choose to design your own...