-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmydict.py
98 lines (71 loc) · 2.54 KB
/
mydict.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
class MyDict:
LOAD_FACTOR_THRESHOLD = 0.7
def __init__(self, initial_size=8):
self._buckets = [[] for _ in range(initial_size)]
self._size = 0
def _hash(self, key):
return hash(key) % len(self._buckets)
def _resize_and_rehash(self):
new_buckets = [[] for _ in range(len(self._buckets) * 2)]
for bucket in self._buckets:
for key, value in bucket:
new_bucket_index = hash(key) % len(new_buckets)
new_buckets[new_bucket_index].append((key, value))
self._buckets = new_buckets
def _get_load_factor(self):
return self._size / len(self._buckets)
def _check_load_factor(self):
load_factor = self._get_load_factor()
if load_factor > self.LOAD_FACTOR_THRESHOLD:
self._resize_and_rehash()
def _get_bucket(self, key):
bucket_index = self._hash(key)
bucket = self._buckets[bucket_index]
return bucket
def __setitem__(self, key, value):
bucket = self._get_bucket(key)
for i, (k, v) in enumerate(bucket):
if k == key:
bucket[i] = (key, value)
return None
bucket.append((key, value))
self._size += 1
self._check_load_factor()
def __getitem__(self, key):
bucket = self._get_bucket(key)
for k, v in bucket:
if k == key:
return v
return None
def __delitem__(self, key):
bucket = self._get_bucket(key)
for i, (k, v) in enumerate(bucket):
if k == key:
del bucket[i]
self._size -= 1
return None
def __contains__(self, key):
bucket = self._get_bucket(key)
for k, v in bucket:
if k == key:
return True
return False
def keys(self):
return [k for bucket in self._buckets for k, v in bucket]
def values(self):
return [v for bucket in self._buckets for k, v in bucket]
def items(self):
return [(k, v) for bucket in self._buckets for k, v in bucket]
def __str__(self):
items = ", ".join(f"{repr(k)}: {repr(v)}" for bucket in self._buckets for k, v in bucket)
return f"{{{items}}}"
my_dict = MyDict()
my_dict['name'] = 'Alice'
my_dict['age'] = 30
assert my_dict['name'] == 'Alice'
assert ('city' in my_dict) is False
assert my_dict['non_existing_field'] is None
del my_dict['non_existing_field']
del my_dict['age']
assert my_dict.keys() == ['name']
assert my_dict.values() == ['Alice']