Question: Write the functions in python please and include explanations and screenshots Question 1a) Define a class named HashTable to represent a hash table. The HashTable
Write the functions in python please and include explanations and screenshots
Question 1a)
Define a class named HashTable to represent a hash table. The HashTable class contains the following methods:
- A private data field named __size that defines the size of a hash table.
- A private data field named __slots that defines a list containing a number of slots of a hash table. The number of slots is the size of a hash table. You can assume that the data stored in the hash table are integers. The underlying data structure of a hash table is a python list.
- A constructor/initializer which takes an integer as a parameter and creates a hash table with the size specified by the integer parameter. The initializer should initialize __slotsto a list of None objects. For example, if the size of a hash table is 5, then the __slots is [None, None, None, None, None]. The None value will be used to represent empty positions in a hash table. The default size is 7.
- The __str__(self) method which returns a string representation of the object formatted as in the examples below.
- The get_hash_code(self, key) method which takes an integer as a parameter and returns a hash code value based on the parameter key. The method should use this formula to calculate the value: key % (size of hash table).
| Test | Result |
my_hash_table = HashTable() print(my_hash_table) print(type(my_hash_table)) print(my_hash_table.get_hash_code(12)) | [None, None, None, None, None, None, None] |
my_hash_table = HashTable(3) print(my_hash_table) print(my_hash_table.get_hash_code(14)) | [None, None, None] 2 |
Question 1b)
Continuing on with your HashTable class implementation, extend the HashTable class by adding the following methods:
- The put(self, key) method which takes an integer as a parameter and inserts the parameter key into a hash table. For simplicity, we assume that the key and value are the same in this question and you may also assume that no collisions occur and there is enough space for the value.
- The __len__(self) method which returns the number of elements present in a hash table.
| Test | Result |
my_hash_table = HashTable(13) my_hash_table.put(26) my_hash_table.put(54) my_hash_table.put(94) my_hash_table.put(17) print(my_hash_table) print("The hash table contains {} items.".format(len(my_hash_table))) | [26, None, 54, 94, 17, None, None, None, None, None, None, None, None] The hash table contains 4 items. |
Question 1c)
Continuing on with your HashTable class implementation,
- The get_new_hash_code_linear_probing(self, index) method which takes an integer as a parameter and uses the linear probing technique to determine where the key should be placed in the hash table when a collision occurs.
- Modify the put(self, key) method. The method should call the get_new_hash_code_linear_probing() method to calculate the next available index position when a collision occurs.
Note: You may assume there will be space in the hash table for the key.
| Test | Result |
my_hash_table = HashTable(13) my_hash_table.put(13) my_hash_table.put(26) my_hash_table.put(14) my_hash_table.put(15) my_hash_table.put(16) my_hash_table.put(17) print(my_hash_table) | [13, 26, 14, 15, 16, 17, None, None, None, None, None, None, None] |
my_hash_table = HashTable(13) my_hash_table.put(12) my_hash_table.put(24) my_hash_table.put(36) my_hash_table.put(48) my_hash_table.put(31) my_hash_table.put(77) my_hash_table.put(43) |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
