This commit is contained in:
20
shared/notes-and-examples/2026.03.04/dictionary.py
Normal file
20
shared/notes-and-examples/2026.03.04/dictionary.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
class Dictionary:
|
||||||
|
backing: "list" = [None] * 26
|
||||||
|
|
||||||
|
def hash(self, key: str):
|
||||||
|
return ord(key) - 97
|
||||||
|
|
||||||
|
def get(self, key: str):
|
||||||
|
index = self.hash(key)
|
||||||
|
return self.backing[index]
|
||||||
|
|
||||||
|
def set(self, key: str, value):
|
||||||
|
index = self.hash(key)
|
||||||
|
self.backing[index] = value
|
||||||
|
|
||||||
|
sample_dict = Dictionary()
|
||||||
|
sample_dict.set("h", "hello")
|
||||||
|
sample_dict.set("i", "hello")
|
||||||
|
print(sample_dict.get("j"))
|
||||||
|
print(sample_dict.get("i"))
|
||||||
|
|
||||||
7
shared/notes-and-examples/2026.03.04/homework.py
Normal file
7
shared/notes-and-examples/2026.03.04/homework.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Homework:
|
||||||
|
# Make the Dictionary class work for any length string. Use any method you can think of.
|
||||||
|
|
||||||
|
# The following should work:
|
||||||
|
# sample_dict = Dictionary()
|
||||||
|
# sample_dict.set("test string", "test value")
|
||||||
|
# sample_dict.set("foo", "bar")
|
||||||
Reference in New Issue
Block a user