Question
PYTHON 3 PLEASE FOLLOW INSTRUCTIONS COMPLETE CODE ONLY DO #TODO PORTIONS OF CODE Problem Complete the implementation of the HashTable class to support the following
PYTHON 3 PLEASE FOLLOW INSTRUCTIONS
COMPLETE CODE
ONLY DO #TODO PORTIONS OF CODE
Problem
Complete the implementation of the HashTable class to support the following functions:
get_node_at_key()
This function takes in a key, and returns the node associated with the key if it exists, or None if the key doesn't exist.
hash()
This function takes in a key (integer only) and returns the hashed value for the given key. You must use the hashing mechanism we discussed in class. Pay attention to the variable INTERNAL_ARRAY_SIZE, which stores the size of the internal array that holds the data of this HashTable.
CODE:
class Node: def __init__(self, key, value): self.key = key self.value = value self.next = None
INTERNAL_ARRAY_SIZE = 500 class HashTable: def __init__(self): self.array = [] for i in range(INTERNAL_ARRAY_SIZE): self.arraya.append(None) def set(self, key, value): node = self.get_node_at_key(key) if node: node.value = value return new_node = Node(key, value) index = self.hash(key) if self.array[index] is None: self.array[index] = new_node else: new_node.next = self.array[index] self.array[index] = new_node def get(self, key): node = self.get_node_at_key(key) return None if not node else node.value def hash(self, key): # TODO def get_node_at_key(self, key): # TODO ask me for a hint if you need my_table = HashTable() my_table.set(0, "Zero") my_table.set(50, "Fifty") my_table.set(500, "Five hundred") my_table.set(50, "Fiftyyyyy") print(my_table.get(50)) # should print "Fiftyyyyy" print(my_table.get(500)) # should print "Five hundred" my_table.set(500, "Zero") print(my_table.get(500)) # should print "Zero"
1 class Node: 2 def __init__(self, key, value): self.key key self.next = None 7 INTERNAL-ARRAY-SIZE= 500 8 class HashTable: 9 def __init (self): 10 self.array for i in range(INTERNAL_ARRAY_SIZE): 12 13 14 def set(self, key, value): 15 16 17 18 19 20 21 self.arraya.append(None) node self.get_node_at_key(key) if node: node.valuevalue return new-node = Node(key, value) index self.hashCkey) if self.arrayLindex] is None: 23 self.array[index] new-node = else: 25 26 27 28 def get(self, key): 29 30 31 32 def hash(self, key): new_node.next self.array [index] self.array[index] new_node node self.get_node_at_key(key) return None if not node else node.value #TOD0 34 35 def get_node_at_key(self, key)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