Skip to content

Instantly share code, notes, and snippets.

@keyan
Created January 27, 2021 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keyan/d840ed26537aefdb94bbd57fa3fcf43d to your computer and use it in GitHub Desktop.
Save keyan/d840ed26537aefdb94bbd57fa3fcf43d to your computer and use it in GitHub Desktop.
example of a simple hash function for strings
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