Python programming assignment | Computer Science homework help

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”)







Place your order
(550 words)

Approximate price: $22

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more

Get 15% OFF on your FIRST order. Use the coupon code: new15