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.

 PYTHON 3 PLEASE FOLLOW INSTRUCTIONS COMPLETE CODE ONLY DO #TODO PORTIONS

OF CODE Problem Complete the implementation of the HashTable class to support

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

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 Databases Questions!