Question
In this week's lab, you are given a hash table that has entries for all time favorite movies according to IMDB. The hashing function for
In this week's lab, you are given a hash table that has entries for "all time favorite" movies according to IMDB. The hashing function for the keys is quite simple for this lab. The table has a fixed size of 10 and it is 70% full. Your first task is to come up with entries with your ownn favorite movies that will cause collisions in the hash table. You'll need to change the program so that it outputs the number of collisions. Then, come up with your own hash function that results in a lower collision rate with the same entries
Python file:
class StringHash: def __init__(self, table_size): self.table_size = table_size self.keys = [''] * table_size self.values = [''] * table_size self.count = 0
def hash(self, elem): return sum(map(lambda x: ord(x), elem))
def add(self, key, value): try: assert type(key) == str and type(value) == str if self.count < self.table_size: slot = self.hash(key) % self.table_size if self.keys[slot] == '': self.keys[slot] = key self.values[slot] = value self.count += 1 return True else: print('Collision b/w ' + self.keys[slot] + ' and ' + key) return False else: print('Hash table is full!!!') return False except AssertionError: print('Element should be string')
def __str__(self): return str(dict(zip(self.keys, self.values)))
def main(): hash_table = StringHash(6) hash_table.add('ECyGu', 'The Shawshank Redemption') hash_table.add('cnHeg', 'The Godfather') hash_table.add('UdXEA', 'The Dark Night') hash_table.add('cgnON', 'Shutter Island') hash_table.add('PFSwE', 'Schindler\'s List') hash_table.add('biFYv', 'Pulp Fiction') hash_table.add('XCTEa', 'Star Wars') print(hash_table) if __name__ == '__main__': main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started