-
-
Save monkey-codes/5d8edc44f7f4f66f551dcc44c1835642 to your computer and use it in GitHub Desktop.
Simple hash table implementation
This file contains hidden or 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
| 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