All checks were successful
Deploy to birb co. production / deploy (push) Successful in 10s
45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
# some variants of this:
|
|
# "quad" probing: use an exponential instead of just adding
|
|
|
|
class Dictionary:
|
|
backing: "list" = [None] * 100
|
|
|
|
def hash(self, key: str):
|
|
ord_total = 0;
|
|
for letter in key:
|
|
ord_total += ord(letter)
|
|
return ord_total % len(self.backing)
|
|
|
|
def get_actual_index_recursion(self, key: str, num_attempts = 0):
|
|
index = self.hash(key) + num_attempts
|
|
|
|
if self.backing[index] is not None and self.backing[index][0] != key:
|
|
return self.get_actual_index_recursion(key, num_attempts + 1)
|
|
|
|
return index
|
|
|
|
def get_actual_index_loop(self, key: str):
|
|
# the loop version; works pretty much the same
|
|
index = self.hash(key)
|
|
while self.backing[index] is not None and self.backing[index][0] != key:
|
|
index += 1
|
|
|
|
return index
|
|
|
|
def get(self, key: str):
|
|
index = self.get_actual_index_recursion(key)
|
|
return self.backing[index][1]
|
|
|
|
def set(self, key: str, value):
|
|
index = self.get_actual_index_recursion(key)
|
|
# instead of storing just values, store tuples (key-value pairs)
|
|
self.backing[index] = (key, value)
|
|
|
|
sample_dict = Dictionary()
|
|
sample_dict.set("test string", "test value")
|
|
sample_dict.set("tets string", "this value will *not* overwrite the other one")
|
|
print(sample_dict.get("test string"))
|
|
print(sample_dict.get("tets string"))
|
|
|
|
print(sample_dict.backing)
|