Update (10/24): 

  • The file hashtable.py has been updated and now includes some code to get started with unittest (go to MS Teams to download it).
  • You will want to download hashtable.py and add onto it, rather than importing it from a new file you create (since you won’t be able to extend the class across separate files).
  • Yes, there is some redundancy between unit tests and the file test_hashtable.py. I am using test_hashtable.py in order to both test and interact with your class (more easily). If your unit tests are thorough/comprehensive, then it will likely look similar to the final version of test_hashtable.py that I will use. 

The tasks in this assignment are to extend the HashTable class that is currently defined in the file “hashtable.py” (located in MS Teams -> General -> Files -> Code). 

By extending it we will increase the functionality of the HashTable class (by adding more methods) and will make it more flexible (e.g. can resize itself as needed) and robust (e.g. no issues/errors when it is full and an item is added). 

The ways to extend the class, and thus the requirements for this assignment are as follows. 

1. Override Python len() function to work with HashTable in a specific way – Using def __len__ will override the Python len() function. The way this is implemented should be to return the number of items stored in the hash table (note: this is *not* the same as HashTable’s “size” field). 

2. Override Python in operator – This will be done using def __contains__ and should be implemented so this operator will then return a boolean when used in statements of the form  54 in myhashtable. For this example, the function should return True if 54 is an existing key in the HashTable instance myhashtable, and False if it is not. 

3. Override Python del operator – This is done by  using def __delitem__ and should allow for a key/value pair to be removed from an instance of HashTable (e.g. del h[54]). Think about how you want to handle cases when deleting a key that was involved with collisions. Be sure to include in the documentation for this method any assumptions/restrictions on how this can be used. 

4. Modify exiting put method to resize an instance as needed – The current HashTable implementation is limited in that it is not able to gracefully handle the case when an item is added to already full HashTable instance. This method should be modified to deal with this more gracefully, and should resize the instance for the user. Be sure to include documentation here describing exactly when you choose to resize and how you select an appropriate new size (remember that prime numbers are preferred for sizes, in order to avoid clustering, or an even more severe but subtle issue). 

All of those methods should be clearly documented to describe how and why the implementation is defined as it is. You will also want to use unittest on this assignment. One good use case for unittest will be to add a few items (and maybe even delete) one, and then to use unittest to verify that the slots and data fields are equal to what you expect. All of the extensions that you’ve added should be tested as well.  

Lastly, for grading, you will want to ensure that you’re program can be run through a test script. The test script will look like the code shown at bottom of this assignment description. When it is run we should expect output similar to what is shown in this screenshot.

test_hashtable.py (the final test script will vary slightly from this):

from <<your file name (without .py)>> import HashTable

# instantiate a HashTable object
h = HashTable(7)

# store keys and values in the object
h[6] = ‘cat’
h[11] = ‘dog’
h[21] = ‘bird’
h[27] = ‘horse’

print(“-“*10, “keys and values”, “-“*10)
print(h.slots)
print(h.data)

# check that data was stored correctly
print(“-“*10, “data check”, “-“*10)
if h.data == [‘bird’, ‘horse’, None, None, ‘dog’, None, ‘cat’]:
    print(”    + HashTable ‘put’ all items in correctly”)
else:
    print(”    – items NOT ‘put’ in correctly”)

# check that ‘in’ operator works correctly
print(“-“*10, “in operator”, “-“*10)
if 27 in h:
    print(”    + ‘in’ operator correctly implemented”)
else:
    print(”    – ‘in’ operator NOT working”)

# delete operator
del h[11]

# check that len() function is implemented and works
print(“-“*10, “len() function”, “-“*10)
if len(h) == 3:
    print(”    + ‘len’ function works properly”)
else:
    print(”    – ‘len’ function NOT working”)

# “in” operator (returns a boolean)
print(“-“*10, “len() after deletion”, “-“*10)
if 11 not in h:
    print(”    + ‘in’ operator works correctly after 11 was removed”)
else:
    print(”    – ‘in’ operator OR ‘del’ NOT working”)

# check that data was also removed
print(“-“*10, “data after deletion”, “-“*10)
if h.data == [‘bird’, ‘horse’, None, None, None, None, ‘cat’]:
    print(”    + data is correct after deletion”)
else:
    print(”    – data not correctly removed after deletion”)

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *