All checks were successful
Deploy to birb co. production / deploy (push) Successful in 10s
33 lines
855 B
Python
33 lines
855 B
Python
# 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")
|
|
|
|
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(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("test string", "test value")
|
|
sample_dict.set("foo", "bar")
|
|
print(sample_dict.get("test string"))
|
|
print(sample_dict.get("foo"))
|
|
|
|
print(sample_dict.backing)
|