0

I need help writing a hash function. I don't really understand them too much but I need to make one for a list of words. I'm writing a program that finds every word in a Word Search that appears in the Word Search's "dictionary". For example if the category of the puzzle was "food" then some words in the dictionary may be: apple, carrot, orange, ect. I need to do this by both double hashing and linear probing which I think I understand but I don't know how to make a good hash function to do so. Can anyone help>

4
  • why cannot you use the existing hashtable/map? Commented Feb 20, 2012 at 21:43
  • 1
    This sounds like homework; if it is, then please edit your question to add the "homework" tag. Commented Feb 20, 2012 at 21:44
  • This looks like homework. If so, you might be able to just use the built in String hash function, which is pretty good. I'd imagine you can then focus on the hash table itself. If I'm wrong about the homework or being allowed to use a String hash function, let me know and I'll try to help more. Commented Feb 20, 2012 at 21:45
  • 1
    You only need to do those things (i.e: double hashing and linear probing) in the event of a collision. The basic process will be to compute the hash of some string then store it in an array or table using the hashcode as the index. Commented Feb 20, 2012 at 21:45

1 Answer 1

2

As far as I understand, you need to build a hash function for a set of words, right? Simple sequential XORing (+ rotating, if the word order matters) of hashCode()'s for each word will do a good job for you.

If unsure, create a class for which you need to build a hash function and execute the Source - Generate hashCode() and equals() command in Eclipse for this class.

Sign up to request clarification or add additional context in comments.

1 Comment

To check your collision resolution, use a terrible hash function like everything mapping to bucket #1 and run it in the debugger. That way you can see how your linear probing functions, at the very least. A great hash function like Alex posted will hide this from you perhaps, stealing a learning opportunity from you but reaching your endgoal quite nicely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.