Answered step by step
Verified Expert Solution
Question
1 Approved Answer
// 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) { 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 airworthyStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started