0

Using the numbers 0-5 I need to write a perfect has function for 36 Student ID Strings with six characters and 3 integers (e.g. BYPLOK120).

The hash function looks something like as follows:

String [] studentID = ...

is 36 unique Strings in the format (xxx/yyy/zzz) where x and y are three letters from the students last and first names, respectively. z are 3 random numbers.

int hashTableSize = 37;`

int [] hashValue = new int[9];`

int [] weights = {1,0,5,4,3,2,5,1,2};  

Weights is 9 numbers between 0-5. THIS IS THE PART I NEED TO FIGURE OUT - there are about 2 million combinations (5^9) and less than 10 will give you a perfect hash function.

for (int i = 0; i < hashTableSize; i++)
   for (int j = 0; j < studentID[I].length; j++)
       hashVal[i] += ( studentID[i].charAt(j) * weight[j] ) % hashTableSize;

I need the hash value of each string in the array to be unique (i.e. there are no collisions when inserting)

I had duplicate hash values as I could not find the unique array of weight integers.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Bopolop
  • 11
  • How long does it take to try one set of weights? If sufficiently short, you can brute-force it. – passer-by Mar 31 '22 at 11:54
  • Is there some principle talked about in class which would help you divine what those 9 numbers are, or are you just supposed to have the clairvoyance to figure out what that principle is? – Robert Harvey Mar 31 '22 at 11:54
  • they reckon were mathemagicians – Bopolop Apr 01 '22 at 07:34
  • @RobertHarvey ^^ – Bopolop Apr 01 '22 at 07:42
  • @passer-by how? – Bopolop Apr 01 '22 at 07:42
  • Generate the first possibility for the weights[] array. Use it to hash all student info - you can stop on the first collision. If no collision, you're done. Otherwise generate the next possibility for weights[] and try again. Repeat until you find a value with no collisions. – passer-by Apr 01 '22 at 12:12
  • The possibilities for weights , are just 0,0,0,0,0,0,0,0,0 then 0,0,0,0,0,0,0,0,1 then ... 0,0,0,0,0,0,0,1,0 etc. It's just counting in a funny format. I'd be tempted to regard it as a single integer that I'd then split into base-6 digits (use Integer.toString(N, 6)), this makes the "generate next" straightforward. – passer-by Apr 01 '22 at 12:16

0 Answers0