In Lecture 8, we are shown a hashing implementation (2nd one) which has trick for converting ch to ints. A loop (for c in str) has:
val = val + shift*ord(c)
I understand the ord() part, I'm just wondering why a multiplier (shift) is applied as well before adding to the val sum? Why not just use ord(c)?
MIT 6.00 Intro Computer Science (OCW)
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
I can only guess that the shift is there to ensure that converting different strings produces different values. (I assume we don't want to end up with fake positive results when searching for certain strings) But I don't know how exactly the shift is solving the problem and Guttag really did not elaborate on that...
That was my guess, too, but I don't see a guarantee of uniqueness there.