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 __init__(self, key, value):
self.key = key
self.value = value
self.next = None
def __str__(self):
return ""%(self.key, self.value,self.next != None)
def __repr__(self):
return str(self)
class HashTable:
def __init__(self, initial_capacity=50):
self.capacity = initial_capacity
self.size =0
self.buckets =[None]*self.capacity
def hash(self, 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 _resize_and_rehash(self):
"""Resize the hash table and rehash all key-value pairs."""
pass
def insert(self, key):
"""Insert a key-value pair into the hash table."""
# 1. Increment size
self.size +=1
# 2. Compute index of key
index = self.hash(key)
# Go to the node corresponding to the hash
node = self.buckets[index]
# 3. If bucket is empty:
if node is None:
# Create node, add it, return
self.buckets[index]= Node(key, key)
return
# 4. 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 key/value
prev.next = Node(key, key)
# Remove node stored at key
# Input: key - int
# Output: removed data value or None if not found
def remove(self, key):
"""
Removes a key-value pair from the hash table.
Args:
key: The key of the key-value pair to be removed.
Returns:
The value associated with the removed key, or None if the key is not found.
"""
pass
def describe_table(self):
'''Will return a list of tuples where each tuple is a key/value 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.append((node.key, node.value))
node = node.next
# Sort the table list by the key
table.sort(key=lambda x: x[0])
return table
def find(self, 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.
"""
# 1. Compute hash
# 2. Go to the node corresponding to the hash
# 3. Traverse the linked list until node found or end of list
# 1. Compute hash
index = self.hash(key)
# 2. Go to first node in list at bucket
node = self.buckets[index]
# 3. Traverse the linked list at this node
while node is not None and node.key != key:
node = node.next
# 4. Now, node is the requested key/value pair or None
if node is None:
# Not found
return None
else:
# Found - return the data value
return node.value
if __name__=="__main__":
ht = HashTable()
# ----- Current implementation -----
ht.insert(4242424242424242)
ht.find(4242424242424242) # returns 4242424242424242
print(ht)
# -----Your implementation (uncomment these to test your code as you make progress)-----
# ht.insert(4242424242424242, "Jonathan") # 4242424242424242 is the key, "Jonathan" is the value
# ht.insert(4444444444444448, "Joseph")
# ht.insert(5555555555555557, "Jotaro")
# ht.insert(5757575757575757, "Josuke")
# ht.insert(1111111111111111, "Giorgio")
# ht.insert(3333333333333333, "Joline")
# print(ht.describe_table())
# print(ht.find(4242424242424242))
# print(ht.find(4444444444444448))
# print(ht.find(5555555555555557))
# print(ht.find(5757575757575757))
# print(ht.find(1111111111111111))
# print(ht.find(3333333333333333))
# ht.remove(4242424242424242)
# ht.remove(4444444444444448)
# ht.remove(5555555555555557)
# ht.remove(5555555555555557)
# ht.remove(5757575757575757)
# print(ht.find(4242424242424242)) # should return None
# print(ht.find(4444444444444448)) # should return None
# print(ht.describe_table())
hash table problem in Python. Here is the code

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!