-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive.py
35 lines (28 loc) · 914 Bytes
/
naive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
""" A dictionary with string keys. This implementation that avoids conveniences like `index` and `enumerate`. """
class EmptyClass:
def __repr__(self):
return "Empty"
Empty = EmptyClass()
def array_index(array, key):
for i in range(len(array)):
if array[i] == key:
return i
raise ValueError(f"Value {key} not found.")
class NaiveDict:
def __init__(self, size):
self.keys = [Empty] * size
self.values = [Empty] * size
def __getitem__(self, key):
return self.values[array_index(self.keys, key)]
def __setitem__(self, key, value):
try:
i = array_index(self.keys, key)
except ValueError:
i = array_index(self.keys, Empty)
self.keys[i] = key
self.values[i] = value
if __name__ == '__main__':
d = NaiveDict(100)
d["hello"] = 10
d["world"] = "foo"
print(d["hello"])