Skip to content

Instantly share code, notes, and snippets.

View AdamGold's full-sized avatar
🌏
Trust the process

Adam Goldschmidt AdamGold

🌏
Trust the process
  • Israel
View GitHub Profile
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"""
def __init__(self, size=10):
self.entries = [None for _ in range(size * 2)]
self.actual_size = size * 2
self.size = size
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
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:
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
@property
def hash(self) -> int:
"""return entry's hash"""
return len(self.key)
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)
int_array = array.array("i", range(10 ** 5))
d = dict.fromkeys(int_array, None)
l = list(range(10 ** 5))
found = 0
for num in int_array:
if num in d:
found += 1
table = HashTable()
names = [
"Eden Marriott",
"Sebastian Frame",
"Cassia Hicks",
"Bert Blankenship",
"Clarence Hughes",
"Clay Burch",
"Linda Holmes",
"Felix Galloway",