Skip to content

Commit 0840d71

Browse files
committed
add dict
1 parent 93f0c59 commit 0840d71

File tree

11 files changed

+52
-0
lines changed

11 files changed

+52
-0
lines changed

ehco/__init__.py

Whitespace-only changes.
File renamed without changes.
File renamed without changes.

ehco/dict/__init__.py

Whitespace-only changes.

ehco/dict/dict.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from ehco.hashtable.hashtable_adt import HashTable
2+
3+
4+
class DictADT(HashTable):
5+
6+
def _iter_slot(self):
7+
for slot in self._table:
8+
if slot not in (HashTable.EMPTY, HashTable.UNUSED):
9+
yield slot
10+
11+
def __setitem__(self, key, value):
12+
self.add(key, value)
13+
14+
def __getitem__(self, key):
15+
if key not in self:
16+
raise KeyError()
17+
else:
18+
return self.get(key)
19+
20+
def items(self):
21+
for slot in self._iter_slot():
22+
yield (slot.key, slot.value)
23+
24+
def keys(self):
25+
for slot in self._iter_slot():
26+
yield slot.key
27+
28+
def values(self):
29+
for slot in self._iter_slot():
30+
yield slot.value
31+
32+
33+
def test_dict_adt():
34+
import random
35+
36+
d = DictADT()
37+
38+
d['a'] = 1
39+
assert d['a'] == 1
40+
d.remove('a')
41+
42+
assert len(d) == 0
43+
44+
l = list(range(30))
45+
random.shuffle(l)
46+
for i in l:
47+
d.add(i, i)
48+
49+
for i in range(30):
50+
assert d.get(i) == i
51+
52+
assert sorted(list(d.keys())) == sorted(l)

ehco/hashtable/__init__.py

Whitespace-only changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)