-
-
Notifications
You must be signed in to change notification settings - Fork 806
Expand file tree
/
Copy pathbag_adt.py
More file actions
43 lines (28 loc) · 695 Bytes
/
bag_adt.py
File metadata and controls
43 lines (28 loc) · 695 Bytes
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
# coding: utf8
class Bag(object):
def __init__(self, maxsize=10):
self.maxsize = maxsize
self._items = list()
def add(self, item):
if len(self) >= self.maxsize:
raise Exception('Full')
self._items.append(item)
def remove(self, item):
self._items.remove(item)
def __len__(self):
return len(self._items)
def __iter__(self):
for item in self._items:
yield item
def test_bag():
bag = Bag()
bag.add(1)
bag.add(2)
bag.add(3)
assert len(bag) == 3
bag.remove(3)
assert len(bag) == 2
for i in bag:
print(i)
if __name__ == '__main__':
test_bag()