Hash Codes and Hash Functions
Example: Hash Functions for Scanner Codes
A hash function deterministically maps each key to an integer in a specified range. This example follows a warehouse scanner that converts two-letter item codes into indices from 0 through 10.
Example: The scanner accepts uppercase letters and assigns A = 1, B = 2, continuing through Z = 26. For a key s, let x be the first letter's value and y the second letter's value. It first calculates an integer hash code C(s), then reduces that code:
Modulo returns the nonnegative remainder after division by 11. All calculations use exact integer arithmetic, with no overflow. Use D = 4, L = 12, and W = 23 for the following parts.
a) Find the index for DL.
For DL, the first letter contributes 31⋅4, which is ________. Add the second letter's value:
The largest multiple of 11 no greater than 136 is ________, obtained from 11⋅12. Subtract it to find the remainder:
The index for DL is 4. Indices are dimensionless, and 4 lies within the required range from 0 through 10.
b) Find the index for LD. What does reversing the letters change?
For LD, the first letter is L, so its weighted contribution is ________. The complete integer code is ________:
Reduce this code using 11⋅34=374:
The index for LD is 2. Reversing the letters changes which value receives the factor of 31. Here, both the integer code and the final index change. Position weighting makes the function sensitive to order, although it does not guarantee different indices for every reversed pair.
c) Find the index for DW. Does sharing an index with DL make the function invalid?
For DW, the integer code is ________:
Using 11⋅13=143, reduce the code:
The index for DW is 4, and this does not make the function invalid. A collision occurs when distinct keys receive the same hash output. DL and DW are different keys, yet both receive index 4 because their integer codes differ by exactly 11.
The function remains deterministic, and repeated equal keys still produce equal results. A matching index does not establish key equality. Reducing many possible keys to eleven indices allows distinct keys to share an output.
A key-processing system first produces an integer and then converts it to a valid array position. Match each term with the description that best identifies its role or property.
Repeated evaluations with the same input and unchanged settings producing the same result.
Two distinct inputs producing the same output from the function under discussion.
An integer produced from a key before any separate conversion to an array position.
The conversion of an integer into one of the permitted array positions.
Collision
Hash code
Range reduction
Determinism
A parcel sorter hashes a three-digit label by processing its digits from left to right. Start with a0=0. For each digit, use its numeric value dj in
Here, modulo returns the nonnegative remainder, and all arithmetic is exact. What final index does the label 827 receive? Give an exact integer with no units, to zero decimal places.