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, size): | |
| """initialise hash table""" | |
| def __getitem__(self, key: str) -> Entry: | |
| """subscript method - object[key] | |
| use hashing function to find the index""" | |
| def __setitem__(self, key: str, value: Any) -> int: | |
| """allow insertion of items with object[key] = value""" |
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
| def __init__(self, size=10): | |
| self.entries = [None for _ in range(size * 2)] | |
| self.actual_size = size * 2 | |
| self.size = size |
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
| def __setitem__(self, key: str, value: Any) -> int: | |
| """allow insertion of items with object[key] = value""" | |
| entry = Entry(key, value) | |
| index = entry.hash % self.actual_size | |
| existing_entry = self.entries[index] | |
| while existing_entry and not entry.compare_hash(existing_entry): | |
| # don't overlap existing values | |
| index = (index + 1) % self.actual_size | |
| existing_entry = self.entries[index] | |
| self.entries[index] = entry |
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
| def __getitem__(self, key: str) -> Entry: | |
| """subscript method - object[key] | |
| use hashing function to find the index""" | |
| entry = Entry(key) | |
| index = entry.hash % self.actual_size | |
| found = self.entries[index] | |
| while found and not entry.compare_hash(found): | |
| index = (index + 1) % self.actual_size | |
| found = self.entries[index] | |
| if found == None: |
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
| def compare_hash(self, other: object) -> bool: | |
| """compare keys and hashes with different entry""" | |
| if not isinstance(other, Entry): | |
| return NotImplemented | |
| return self.key == other.key and self.hash == other.hash |
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
| @property | |
| def hash(self) -> int: | |
| """return entry's hash""" | |
| return len(self.key) |
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 Entry(object): | |
| def __init__(self, key: str, value=None): | |
| self.key = key | |
| self.value = value | |
| @property | |
| def hash(self) -> int: | |
| """return entry's hash""" | |
| return len(self.key) |
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
| int_array = array.array("i", range(10 ** 5)) | |
| d = dict.fromkeys(int_array, None) | |
| l = list(range(10 ** 5)) |
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
| found = 0 | |
| for num in int_array: | |
| if num in d: | |
| found += 1 |
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
| table = HashTable() | |
| names = [ | |
| "Eden Marriott", | |
| "Sebastian Frame", | |
| "Cassia Hicks", | |
| "Bert Blankenship", | |
| "Clarence Hughes", | |
| "Clay Burch", | |
| "Linda Holmes", | |
| "Felix Galloway", |