Question: hash table problem in Python. Here is the code problem: class Node: def _ _ init _ _ ( self , key, value ) :
hash table problem in Python.
Here is the code problem:
class Node:
def initself key, value:
self.key key
self.value value
self.next None
def strself:
return selfkey, self.value,self.next None
def reprself:
return strself
class HashTable:
def initself initialcapacity:
self.capacity initialcapacity
self.size
self.buckets Noneselfcapacity
def hashself key:
Hashes the given key using the modulo operator.
Parameters:
key: The key to be hashed.
Returns:
The hashed value of the key.
return self.capacity key
def resizeandrehashself:
Resize the hash table and rehash all keyvalue pairs."""
pass
def insertself key:
Insert a keyvalue pair into the hash table."""
# Increment size
self.size
# Compute index of key
index self.hashkey
# Go to the node corresponding to the hash
node self.bucketsindex
# If bucket is empty:
if node is None:
# Create node, add it return
self.bucketsindex Nodekey key
return
# Iterate to the end of the linked list at provided index
prev node
while node is not None:
prev node
node node.next
# Add a new node at the end of the list with provided keyvalue
prev.next Nodekey key
# Remove node stored at key
# Input: key int
# Output: removed data value or None if not found
def removeself key:
Removes a keyvalue pair from the hash table.
Args:
key: The key of the keyvalue pair to be removed.
Returns:
The value associated with the removed key, or None if the key is not found.
pass
def describetableself:
Will return a list of tuples where each tuple is a keyvalue pair
DO NOT MODIFY THIS METHOD
table
# Iterate through each bucket in the hash table
for bucket in self.buckets:
# Traverse the linked list at the current bucket
node bucket
while node is not None:
# Append a tuple of key and value to the table list
table.appendnodekey, node.value
node node.next
# Sort the table list by the key
table.sortkeylambda x: x
return table
def findself key:
Finds the value associated with the given key in the hash table.
Parameters:
key any: The key to search for in the hash table.
Returns:
any: The value associated with the given key, or None if the key is not found.
# Compute hash
# Go to the node corresponding to the hash
# Traverse the linked list until node found or end of list
# Compute hash
index self.hashkey
# Go to first node in list at bucket
node self.bucketsindex
# Traverse the linked list at this node
while node is not None and node.key key:
node node.next
# Now, node is the requested keyvalue pair or None
if node is None:
# Not found
return None
else:
# Found return the data value
return node.value
if namemain:
ht HashTable
# Current implementation
htinsert
htfind # returns
printht
# Your implementation uncomment these to test your code as you make progress
# htinsert "Jonathan" # is the key, "Jonathan" is the value
# htinsert "Joseph"
# htinsert "Jotaro"
# htinsert "Josuke"
# htinsert "Giorgio"
# htinsert "Joline"
# printhtdescribetable
# printhtfind
# printhtfind
# printhtfind
# printhtfind
# printhtfind
# printhtfind
# htremove
# htremove
# htremove
# htremove
# htremove
# printhtfind # should return None
# printhtfind # should return None
# printhtdescribetable
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
