Skip to content

String hashing

storing

Level 1 of 3

Also called Horner's rule and Polynomial string hash.

fresh numbers

The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from string hashing's own run on the numbers shown.

choose one

question 1

Which slot does the method give the word ice?

ice#×31+#h0
the answer, and why

the answer is A

i is 9: 0 times 31 plus 9 is 9, which leaves 9. Then 9 times 31 plus 3 is 282, which leaves 9. Then 9 times 31 plus 5 is 284, which leaves 11.

if you chose another

  • That adds the letters, 17, and keeps the remainder. The method multiplies by 31 before each letter.
  • That is the remainder of e's 5 after dividing by 13, the last letter alone. Multiplying by 31 keeps every letter in the value.
  • That reads the letters from the last, e first, as eci. The method starts at the first letter.

new numbersthe test page's numbers

question 2

How many letters does the method read on the word ticket?

ticket#×31+#h0
the answer, and why

the answer is A

One letter read each time round the loop, and ticket has 6.

if you chose another

  • That is 3 for each letter, a multiply, an add and a remainder. Each letter is read once.
  • That is the watch page's steps, 3 a letter, a start and an end.
  • The slot is opened once, but working it out reads every letter.

new numbersthe test page's numbers