solution

Create a python file that uses file with words and hashes them.

  1. Say the word you want to store in the hash table is part
  2. Using the sort function you implement, sort the letters in the word to obtain the key: aprt
  3. Using the hashing function you develop, hash the key “aprt” to compute bucket (or slot) in the hash table where the key will be stored
    1. If that slot is empty, this is the first key to be hashed to that slot. Create a list associated with that slot. Append the key (aprt) to this list. Next, append the word part to the list.
    2. If that slot is not empty, some key has already been hashed to this slot.
  • does the key already stored there equal “aprt”? If so, one or more anagrams of part are already stored there. Append the word partto the list associated with that slot.
  • if the key already stored there does not equal “aprt”, you have a collision – two different keys have been hashed to the same slot. Using your rehash() function, find a new slot for the key “aprt” and continue with step 3a. Your rehash() function may use linear probing or quadratic probing.

Example – Searching

  1. Say the string whose anagrams you seek is part
  2. Using the sort function you implement, sort the letters in the word to obtain the key: aprt
  3. Using the hashing function you develop, hash the key to compute slot in the hash table where the key may be found
      1. Does the key stored there equal “aprt”? If so, return the list associated with that slot. Those are the anagrams of part
      2. If the key stored there does not equal “aprt”, there must have been a collision during the store phase, and the key aprt is stored in some other slot. Using your rehash() function, find the correct slot for the key “aprt” and continue with step 3a.

Details

The hash table code which you develop must have the following functionality:

  • an __init__ method. The HashTable will initially have 31 slots, all initialized to None.
  • a __str__ method that returns a string of the form: x slots, y occupied, load factor = z
  • a store method that accepts a word and stores it in the HashTable as described above. If a conflict is encountered, it uses either linear probing or quadratic probing to resolve the conflict. Following each store, the load factor is calculated. Whenever the load factor exceeds 0.7, the rehashmethod (described below) will be called.
  • a get method that accepts a word and finds and returns its anagramsas described above
  • a hash_function method that accepts a string, hashes it, and returns the corresponding index/slot
  • a rehash function that employs linear probing or quadratic probing.
  • a remap method. This method is called whenever the load factor exceeds 0.7. This method increases the number of slots in the HashTable to a prime number that is roughly twice the current number of slots. Following the resizing, the data already in the HashTable will no longer be at the right locations. So, this method relocates all the data that is already in the HashTable to new locations consistent with the increased size of the HashTable. For example, if the current size of the HashTable is 31 slots, remap will increase the size to 67 and use store to relocate all data currently in the HashTable to new locations.
 
"Looking for a Similar Assignment? Get Expert Help at an Amazing Discount!"
Looking for a Similar Assignment? Our Experts can help. Use the coupon code SAVE30 to get your first order at 30% off!

Hi there! Click one of our representatives below and we will get back to you as soon as possible.

Chat with us on WhatsApp