Answer To: FIT1008 Introduction to Computer Science (FIT2085: for Engineers) Interview Prac 3 - Weeks 11 and 12...
Pritam answered on Jun 13 2021
task1.py
class HashTable:
def __init__(self, table_capacity = 11, hash_base = 31):
self.table_capacity = table_capacity # initialize capacity of hash table
self.key_count = 0 # initialize count of distinct keys stored to 0
self.hash_base = hash_base # initialize the base for computing hash values
self.Primes = [ 3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197,
239, 293, 353, 431, 521, 631, 761, 919, 1103, 1327, 1597, 1931,
2333, 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143,
14591, 17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851,
75431, 90523, 108631, 130363, 156437, 187751, 225307, 270371,
324449, 389357, 467237, 560689, 672827, 807403, 968897, 1162687,
1395263, 1674319, 2009191, 2411033, 2893249, 3471899, 4166287,
4999559, 5999471, 7199369] # store list of primes
self.table = [ (None, None) ]*table_capacity # create an empty table with specified capacity
def __getitem__(self, key):
hash_value = self.hash(key) # compute hash value corresponding to key
if self.table[hash_value][0] != key:# if the key does not match with the key stored in the table location whose index is hash value
raise KeyError('Key is not present in the hash table!') # raise a KeyError
else:
return self.table[hash_value][1] # else return the value stored in the table location whose index is the hash value
def __setitem__(self, key, value):
if self.__contains__(key) == False and self.key_count == self.table_capacity:
self.rehash()
hash_value = self.hash(key) # compute hash value corresponding to key
self.table[hash_value] = (key, value) # store the (key, value) tuple in the table location whose index is the hash value
self.key_count += 1 # increase count of distinct keys stored by 1
def __contains__(self, key):
hash_value = self.hash(key) # compute hash value corresponding to key
if self.table[hash_value][0] == key:# if the key matches with the key stored in the table location whose index is the hash value
return True # return True
else:
return False # else, return False
def hash(self, key):
value = 0
for i in range(len(key)): # compute the hash value using the universal hash function
value = ( value*self.hash_base + ord(key[i]) ) % self.table_capacity
return value
def rehash(self):
new_prime = 0
for i in range(len(self.Primes)):
if self.Primes[i] > self.table_capacity:
new_prime = self.Primes[i]
break
if new_prime == 0:
raise ValueError('No suitable new prime found!')
else:
old_table = self.table
self.table_capacity = new_prime
self.table = [ (None, None) ]*new_prime
for (key, value) in old_table:
self._setitem_(key, value)
task2.py
class HashTable:
def __init__(self, table_capacity = 11, hash_base = 31):
self.table_capacity = table_capacity # initialize capacity of hash table
self.key_count = 0 # initialize count of distinct keys stored to 0
self.hash_base = hash_base # initialize the base for computing hash values
self.Primes = [ 3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197,
239, 293, 353, 431, 521, 631, 761, 919, 1103, 1327, 1597, 1931,
2333, 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143,
14591, 17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851,
75431, 90523, 108631, 130363, 156437, 187751, 225307, 270371,
324449, 389357, 467237, 560689, 672827, 807403, 968897, 1162687,
1395263, 1674319, 2009191, 2411033, 2893249, 3471899, 4166287,
4999559, 5999471, 7199369] # store list of primes
self.table = [ (None, None) ]*table_capacity # create an empty table with specified capacity
def __getitem__(self, key):
hash_value = self.hash(key) # compute hash value corresponding to key
if self.table[hash_value][0] != key:# if the key does not match with the key stored in the table location whose index is hash value
raise KeyError('Key is not present in the hash table!') # raise a KeyError
else:
return self.table[hash_value][1] # else return the value stored in the table location whose index is the hash value
def __setitem__(self, key, value):
if self.__contains__(key) == False and self.key_count == self.table_capacity:
self.rehash()
hash_value = self.hash(key) # compute hash value corresponding to key
self.table[hash_value] = (key, value) # store the (key, value) tuple in the table location whose index is the hash value
self.key_count += 1 # increase count of distinct keys stored by 1
def __contains__(self, key):
hash_value = self.hash(key) # compute hash value corresponding to key
if self.table[hash_value][0] == key:# if the key matches with the key stored in the table location whose index is the hash value
return True # return True
else:
return False # else, return False
def hash(self, key):
value = 0
for i in range(len(key)): # compute the hash value using the universal hash function
value = ( value*self.hash_base + ord(key[i]) ) % self.table_capacity
return value
def rehash(self):
new_prime = 0
for i in range(len(self.Primes)):
if self.Primes[i] > self.table_capacity:
new_prime = self.Primes[i]
break
if new_prime == 0:
raise ValueError('No suitable new prime found!')
else:
old_table = self.table
self.table_capacity = new_prime
self.table = [ (None, None) ]*new_prime
for (key, value) in old_table:
self._setitem_(key, value)
# function that reads a specified file containing one word per line,
# and adds each word to hash_table with integer 1 as the associated data
def load_dictionary(hash_table, filename):
with open(filename, 'r') as f:
words = f.readlines()
for b in [1, 27183, 250726]:
for TABLESIZE in [250727, 402221, 1000081]:
ht = HashTable(TABLESIZE, b)
for word in words:
ht.__setitem__(word, 1)
f.close()
task3.py
class HashTable:
def __init__(self, table_capacity = 11, hash_base = 31):
self.table_capacity = table_capacity # initialize capacity of hash table
self.key_count = 0 # initialize count of distinct keys stored to 0
self.hash_base = hash_base # initialize the base for computing hash values
self.collision_count = 0 # initialize collision_count to 0
self.probe_total = 0 # initialize probe_total to 0
self.probe_max = 0 # initialize probe_max to 0
self.rehash_count = 0 # initialize rehash_count to 0
self.Primes = [ 3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197,
239, 293, 353, 431, 521, 631, 761, 919, 1103, 1327, 1597, 1931,
...