Skip to content

Instantly share code, notes, and snippets.

@stonly
Last active September 22, 2017 14:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stonly/5895765 to your computer and use it in GitHub Desktop.
Save stonly/5895765 to your computer and use it in GitHub Desktop.
hash table demonstration code
#!/usr/bin/python
"""
Hash Tables
Usage :
'python -i hashex.py'
The goal of a hash table is to map a keyword to a placeholder that contains the value you stored.
We will define 3 functions :
hash_string_key(key,hash_size) - illustrating how to take a string and turn it into a refrence of where the key and value may be stored in the hash table. This function also illustrates the use and utility of character ordinals and modulus.
create_hash_table(n) - illustrating how to create the empty lists that will be our hash tabel. This function illustrates a simple list comprehension function.
hash_get_key(h_table,key) - illustrating how to lookup where a key should be located in the hash table.
hash_add_key(h_table,key,value) - illustrating how to add or replace a given key with a new value.
hash_get_key_val(h_table, key) - illustrating how to extract the value of a given key out of the hash table.
Hashable datatypes include strings, integers, and tuples and Python includes a builtin funtction hash() that will give you the python hash of the key. See http://effbot.org/zone/python-hash.htm
hash_key(key, hash_size) - illustrates the usage of type() to determine the datatype of the key and produces the resulting hash if the key is hashable.
The perferred method for creating and managing hash tables is with the built in Python dictionary datatype.
"""
def hash_string_key(key,hash_size):
"""
Takes a string called key and the size of your hash table. Returns a number representing where your key would end up in the hash table.
"""
pos = 0
for s in key:
pos += ord(s)
return pos % hash_size
def create_hash_table(n):
"""
Takes an integer called n. Returns a hash table with n number or place holders (emtpy lists)
"""
return [[] for i in range(n)]
def hash_get_key(h_table,key):
"""
Takes a hash table and the key as input. Returns the current value of the hash table where the key should be located.
"""
h_table_length = len(h_table)
index = hash_key(key,len(h_table))
return h_table[index]
def hash_add_key(h_table,key,value):
"""
Takes a hash table, a key, and a value as input. Returns the updated h_table.
"""
index = hash_key(key,len(h_table))
h_table[index].append([key,value])
return h_table
def hash_get_key_val(h_table,key):
"""
Takes a hash table and the key as input. Returns the current value of the key if found and None if not.
"""
val = hash_get_key(h_table,key)
if val :
for k,v in val:
if k == key : return v
else:
return None
return None
def hash_del_key(h_table,key):
"""
Takes a hash table and the key as input. Returns None but deletes the key if found.
"""
h_index = hash_get_key(h_table,key)
if key_index == None:
pass
else:
k_index = hash_get_key_val(h_table,key)
i= 0
for k,v in k_index:
if k == key:
k_index.pop(i)
break
i += 1
return None
def hash_key(key, hash_size):
key_type = type(key)
if key_type == int:
return key
if key_type == str:
return hash_string_key(key,hash_size)
if key_type == tuple:
p = 0
for item in key :
p += hash_key(item, hash_size)
return p
raise TypeError,"unhashable type: "+type(key).__name__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment