PYTHON Why am I getting an error and it doesn't show the right output?  Problem 1 # Implement a hashtable using an array. Your implementation should include public methods for insertion, deletion, and # search, as well as helper methods for resizing. The hash table is resized when the loadfactor becomes greater than 0.6 # during insertion of a new item. You will be using linear probing technique for collision resolution. Assume the key to # be an integer and use the hash function h(k) = k mod m where m is the size of the hashtable. class HashTableProb:     def __init__(self, size=10):         # Initialize the hashtable with the given size and an empty array to hold the key-value pairs.         self.__size = size # size of the hashtable         self.__hashtable = [None for _ in range(size)]         self.__itemcount = 0 # Keeps track of the number of items in the current hashtable     def __contains__(self, key):         return self.__searchkey(key)     def __next_prime(self, x):         def is_prime(x):             return all(x % i for i in range(2, x))         return min([a for a in range(2*x+1, x*x) if is_prime(a)])     def __hash(self, key):         # Hash function to be used for mapping a key to the indices in the hashtable.         bucket = key % self.__size         return bucket     def __resize(self):         # Resizes the hashtable to the next prime number after current table size. Also, rehashes all the current items.         n = self.__next_prime(self.__size*2)         new = [None for _ in range(n)]         count = 0         while count < n:             if self.__hashtable[count] is not None:                 key = self.__hashtable[count]                 new.insert(key)             count = count + 1         self.__size = n         return new     def insert(self, key, value):         # Insert a new key-value pair (as a python list) into the hashtable. When the loadfactor is greater than 0.6         # resizes the current hash table before inserting the new item.         bucket = self.__hash(key)         checked = 0         m = self.__size         while checked < m:             if self.__hashtable[bucket] is None:                 self.__hashtable[bucket].__searchkey = key                 self.__hashtable[bucket] = value                 self.__itemcount += 1                 loadfactor = self.__itemcount/self.__size                 if loadfactor > 0.6:                     self.__resize()                 return True             bucket = (bucket + 1) % m             checked += 1         return False     def delete(self, key):         # Remove a key-value pair from the hashtable. You need to define a marker to differentiate deleted slots and         # empty slots.         bucket = self.__hash(key)         checked = 0         m = self.__size         while self.__hashtable[bucket] is not None and checked < m:             if self.__hashtable[bucket] != -1 and self.__hashtable[bucket].__searchkey == key:                 self.__hashtable[bucket] = -1                 self.__itemcount -= 1                 return True             bucket = (bucket + 1) % m             checked += 1         return False     def __search(self, key):         # Find the item associated with a given key in the hashtable.         bucket = self.__hash(key)         checked = 0         m = self.__size         while self.__hashtable[bucket] is not None and checked < m:             if self.__hashtable[bucket] != -1 and self.__hashtable[bucket].__searchkey == key:                 return self.__hashtable[bucket]             bucket = (bucket + 1) % m             checked += 1         return False     def showitems(self):         # prints all the items in the hashtable.         count = 0         m = self.__size         while count < m:             print(self.__hashtable[count])             count += 1         return False # Problem 1 Test Code testa = HashTableProb() print("Start") testa.showitems() testa.insert(3, 'first') testa.insert(5, 'second') testa.insert(0, 'third') print("Test insert") testa.showitems() testa.delete(3) print("Test delete") testa.showitems()

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

PYTHON

Why am I getting an error and it doesn't show the right output?

 Problem 1
# Implement a hashtable using an array. Your implementation should include public methods for insertion, deletion, and
# search, as well as helper methods for resizing. The hash table is resized when the loadfactor becomes greater than 0.6
# during insertion of a new item. You will be using linear probing technique for collision resolution. Assume the key to
# be an integer and use the hash function h(k) = k mod m where m is the size of the hashtable.

class HashTableProb:
    def __init__(self, size=10):
        # Initialize the hashtable with the given size and an empty array to hold the key-value pairs.
        self.__size = size # size of the hashtable
        self.__hashtable = [None for _ in range(size)]
        self.__itemcount = 0 # Keeps track of the number of items in the current hashtable

    def __contains__(self, key):
        return self.__searchkey(key)

    def __next_prime(self, x):
        def is_prime(x):
            return all(x % i for i in range(2, x))

        return min([a for a in range(2*x+1, x*x) if is_prime(a)])

    def __hash(self, key):
        # Hash function to be used for mapping a key to the indices in the hashtable.
        bucket = key % self.__size
        return bucket

    def __resize(self):
        # Resizes the hashtable to the next prime number after current table size. Also, rehashes all the current items.
        n = self.__next_prime(self.__size*2)
        new = [None for _ in range(n)]
        count = 0
        while count < n:
            if self.__hashtable[count] is not None:
                key = self.__hashtable[count]
                new.insert(key)
            count = count + 1
        self.__size = n
        return new

    def insert(self, key, value):
        # Insert a new key-value pair (as a python list) into the hashtable. When the loadfactor is greater than 0.6
        # resizes the current hash table before inserting the new item.
        bucket = self.__hash(key)
        checked = 0
        m = self.__size
        while checked < m:
            if self.__hashtable[bucket] is None:
                self.__hashtable[bucket].__searchkey = key
                self.__hashtable[bucket] = value
                self.__itemcount += 1
                loadfactor = self.__itemcount/self.__size
                if loadfactor > 0.6:
                    self.__resize()
                return True
            bucket = (bucket + 1) % m
            checked += 1
        return False

    def delete(self, key):
        # Remove a key-value pair from the hashtable. You need to define a marker to differentiate deleted slots and
        # empty slots.
        bucket = self.__hash(key)
        checked = 0
        m = self.__size
        while self.__hashtable[bucket] is not None and checked < m:
            if self.__hashtable[bucket] != -1 and self.__hashtable[bucket].__searchkey == key:
                self.__hashtable[bucket] = -1
                self.__itemcount -= 1
                return True
            bucket = (bucket + 1) % m
            checked += 1
        return False

    def __search(self, key):
        # Find the item associated with a given key in the hashtable.
        bucket = self.__hash(key)
        checked = 0
        m = self.__size
        while self.__hashtable[bucket] is not None and checked < m:
            if self.__hashtable[bucket] != -1 and self.__hashtable[bucket].__searchkey == key:
                return self.__hashtable[bucket]
            bucket = (bucket + 1) % m
            checked += 1
        return False

    def showitems(self):
        # prints all the items in the hashtable.
        count = 0
        m = self.__size
        while count < m:
            print(self.__hashtable[count])
            count += 1
        return False


# Problem 1 Test Code
testa = HashTableProb()
print("Start")
testa.showitems()
testa.insert(3, 'first')
testa.insert(5, 'second')
testa.insert(0, 'third')
print("Test insert")
testa.showitems()
testa.delete(3)
print("Test delete")
testa.showitems()

 

Expert Solution
steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Potential Method of Analysis
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education