Skip to content

New hashing exercise#21

Open
AlexisEnglebert wants to merge 7 commits intopschaus:mainfrom
AlexisEnglebert:hash-exercices
Open

New hashing exercise#21
AlexisEnglebert wants to merge 7 commits intopschaus:mainfrom
AlexisEnglebert:hash-exercices

Conversation

@AlexisEnglebert
Copy link
Contributor

Hello,

Here is a new exercise about polynomial hash.

The solution idea is to compute the hash for each row with polynomial hash for e.g the string "hello" we have

hash = (h · a⁰ + e · a¹ + l · a² + l · a³ + o · a⁴) mod m (where a is the base and m the modulo)

and then we can rotate the string by simply doing some math operation on the already hashed string so to get "ohell" we remove the o from "hello"

hash = (h · a⁰ + e · a¹ + l · a² + l · a³ ) mod m

Then we multiply the hash by the basis (a)

hash = (h · a⁰ + e · a¹ + l · a² + l · a³ ) · a = (h · a¹ + e · a² + l · a³ + l · a⁴ ) mod m

And finally we add the o at the start:

(h · a¹ + e · a² + l · a³ + l · a⁴ ) + o · a⁰ = ( o· a⁰ + h · a¹ + e · a² + l · a³ + l · a⁴ ) mod m

And now we have the hash for "ohell" in O(1).

@haroldks haroldks marked this pull request as draft November 27, 2025 10:39
@haroldks haroldks marked this pull request as ready for review November 27, 2025 10:39
Copy link
Collaborator

@haroldks haroldks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello Alexis,

Thank you for your exercise. It is a very interesting one. In general, there is too much to do for the students in my opinion. The challenge here should only focus on think about rolling hash to compute the hash of the word rotations.

I would suggest :

  • Giving the student a method or function to compute the hash of the base word
  • In you solution extract the rotation par in a method with relevant comments so when looking at the solution we know what is happening
  • maybe use digit instead of char as input for the solve method to simplify test writings
  • Add a comment for the base exponent precomputation

I also think that it can be possible to track the best while avoiding using pair and comparable in the end. I've add an idea in the comments but we can discuss it further.

@AlexisEnglebert
Copy link
Contributor Author

Hey @haroldks,

I've taken account the proposed modifications for the exercise, I've moved the hashing logic into a separate function and removed the minimum pair things. Now only a valid pair is required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants