Search code examples
stringhashperfect-hash

hashing string to an int between 0-19


I was wondering how I would hash a string value (ex: "myObjectName") to int values between 0-19 I'm guaranteed to have no more than 20 unique string values.

Thanks


Solution

  • Adding my comment as an answer as suggested:

    I would suggest that hashing isn't the exact path you should follow here.

    One method would be using a dictionary (like the built in data structure in Python) that has a key-value pair of your string and a number from 1-20 (or 0 - 19)

    As you read or see each string, you could check to see if a dictionary entry exists, if so, do whatever needs to be done, if not, create a new dictionary entry with the next available number (generated by looking at the number of existing entries in the dictionary).