Created
January 27, 2021 03:46
-
-
Save keyan/d840ed26537aefdb94bbd57fa3fcf43d to your computer and use it in GitHub Desktop.
example of a simple hash function for strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
some_big_prime = 16908799 | |
def hash_string(s: str) -> int: | |
hash_val = 0 | |
for letter in s: | |
hash_val = ((127 * hash_val) + ord(letter)) % some_big_prime | |
return hash_val | |
hash_table = [None for _ in range(size_of_my_hash_table)] | |
value = 'foobar' | |
hash_table[hash_string(value) % size_of_my_hash_table] = value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment