Skip to content

Instantly share code, notes, and snippets.

@monkey-codes
Created May 12, 2017 01:46
Show Gist options
  • Select an option

  • Save monkey-codes/5d8edc44f7f4f66f551dcc44c1835642 to your computer and use it in GitHub Desktop.

Select an option

Save monkey-codes/5d8edc44f7f4f66f551dcc44c1835642 to your computer and use it in GitHub Desktop.
Simple hash table implementation
class HashTable(object):
def __init__(self):
self.table = [None]*10000
def store(self, string):
hash_code = self.calculate_hash_value(string)
bucket = [] if not self.table[hash_code] else self.table[hash_code]
bucket.append(string)
self.table[hash_code] = bucket
def lookup(self, string):
hash_code = self.calculate_hash_value(string)
bucket = self.table[hash_code]
if bucket != None:
if string in bucket: return hash_code
return -1
def calculate_hash_value(self, string):
#Only use first 2 chars to calc hash code
return (ord(string[0])*100) + ord(string[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment