but I have my doubts the professor is correct, shouldn't the sum of 116*31^0 + 104*31^1 + 105*31^2 + 115*31^3 == the hash value by the end of the loop in the algorithm he has wrote?
for 116*31^0 + 104*31^1 + 105*31^2 + 115*31^3 I get the answer 3,425,965 and for the hash in the algorithm I get 3'559'070, the results are very close but do not match
the algorithm is written at 10:12
1 2 3 4 5 6 7 8 9 10 11 12 13
int hashCode(string str){
hash = 0;
int g = 31;
for(int i = 0; i < str.size(); ++i)
hash = g * hash + str.at(i);
return hash;
}
I do not do videos; I can read 3+ pages of text in the time some goober can recite 1/2 a page. I was just giving you the value I got for the numbers.
his point can be correct with bad code or bad algorithm; in fact, this is fairly common in many textbooks :)
the algorithm is either wrong or its an approximation of something, I do not know, I didn't watch it. But if you want that algorithm, why not code it yourself? for i = 0...n hash+= data*pow(31, i) (though I would hard code up a table of powers of 31 for speed if you are doing something for real).
by the way, a hash algorithm that can be expressed in a single line of code is almost always too simple and going to have flaws (it may still be good enough for a microscopic hash table etc in spite of this). Be wary of such things if doing a bigger project.