diff --git a/shared/notes-and-examples/2026.03.18/chaining.py b/shared/notes-and-examples/2026.03.18/chaining.py new file mode 100644 index 0000000..35692a5 --- /dev/null +++ b/shared/notes-and-examples/2026.03.18/chaining.py @@ -0,0 +1,34 @@ +class Dictionary: + backing: "list" = [None] * 100 + + def __init__(self): + for i in range(len(self.backing)): + self.backing[i] = [] + + def hash(self, key: str): + ord_total = 0; + for letter in key: + ord_total += ord(letter) + return ord_total % len(self.backing) + + + def get(self, key: str): + index = self.hash(key) + for item in self.backing[index]: + if item[0] == key: + return item[1] + + return None + + def set(self, key: str, value): + index = self.hash(key) + self.backing[index].append((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)