hashing code required..

hi friends.

Select 30 keywords of C++ language and try to write a hash function that return a unique
number(index value) for every word in the range from 0 to 29.


please send me hash function
closed account (1yR4jE8b)
I'll be nice and point out this part for you specifically from the article:

1
2
3
4
Don't post homework questions
Programmers are good at spotting homework questions; most of us have done them ourselves.
 Those questions are for you to work out, so that you will learn from the experience. 
It is OK to ask for hints, but not for entire solutions. 
Actually also these sections are needed to the OP:
* Before You Ask
* Use meaningful, specific subject headers
* Be precise and informative about your problem
* Be explicit about your question
just give me hint .... or algo
You know the 30 keywords? lol.

The algorithm:
1
2
3
4
5
6
7
8
9
10
   if (keyword == first_keyword)
       return 0;
   else if (keyword == second_keyword)
      return 1;
   else if ...
    ...
   else if (keyword == last_keyword)
      return 29;
    // if no match, return -1 for error 
    return -1;


Thats the easiest thinkable map, you can build for this. (since you have to select these 30 keywords before you go for it...)
You need to search "Minimal Perfect Hash Algorithm".
Last edited on
@maikel,
First of all, that is not a hashing algorithm. All you're doing is returning hardcoded values based on other seemingly hardcoded values. I don't even know what that's meant to be or do. Regardless of that code being totally useless as far as hashing goes, please don't post code for homework questions.
Technically, yes, it is. Although not the type of hash function you're probably thinking of.
A hash function is any well-defined procedure or mathematical function that converts a large, possibly variable-sized amount of data into a small datum, usually a single integer that may serve as an index to an array.
In this case, as Dufresne has pointed out, that's called a "minimal perfect hash".
Since the OP was asked to hash a very specific set of data, a generic hash function would be an unnecessary -- and probably undesirably -- complication.
Last edited on
I assumed OP meant a, for want of a better word, "real" hashing function like SHA* or MD*. One that could be used to hash e.g. passwords, files, etc.

Since the OP was asked to hash a very specific set of data, a generic hash function would be an unnecessary -- and probably undesirably -- complication.

I suppose. My guess was that OP wanted a hashing function that worked generally, but you're probably right.
chrisname wrote:

"real" hashing function like SHA* or MD*


Did you ever heard of "Hash-Tables"s? What do you call maps that hash into a specific set of indeces? IMHO its really funny to speak of "real" hash functions and call SHA* or MD*...

keep cool.

Maikel
closed account (z05DSL3A)
SHA* or MD* are Cryptographic hash function.

Cryptographic hash function are not the same as Hash Functions (although they are related).
Topic archived. No new replies allowed.