Question: // TODO: Implement your hash function! int hash_string (string str) { int output = 0; return output; } The above is what I have so

 // TODO: Implement your hash function! int hash_string (string str) {

// TODO: Implement your hash function! int hash_string (string str) { int output = 0; return output; } 

The above is what I have so far, but I am confused as to how hash functions are written. Any help would be appreciated!

We're going to do something kind of weird; we're going to create something that looks an awful lot like a hash table, using a map (which could be implemented using a hash table!). The important step we'll focus on is making our own hash function. This may sound complicated, but it really comes down to this: we are provided a string, and want to return an integer that somehow represents this string. Here's how we'll do this: 1. Find the string's integer representation by taking the sum of each character (ASCII conversion) in the string This satisfies property #1. 2. Limit the number of keys to 1,000. Accomplish this by using the mod (%) operator on the sum found in step 1. o This satisfies property #2. o Notice that this means we will get a lot of strings mapping to the same key-there are 127,142 words in our dictionary, but only 1,000 keys! 3. Use this resulting key to get the vector in the map, and add this string to its vector. Let's test your implementation! With the string hello , your hash should be 532, and with trustworthy you should get 263. To test your hashed map, verify that with a key of 1, the first string in the list is airworthy

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!