-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathBag3.py
executable file
·178 lines (157 loc) · 4.48 KB
/
Bag3.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python3
# Copyright © 2012-13 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. It is provided for educational
# purposes and is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
class Bag:
def __init__(self, items=None):
"""
>>> bag = Bag(list("ABCDEB"))
>>> bag.count("A"), bag.count("B"), bag.count("Z")
(1, 2, 0)
"""
self.__bag = {}
if items is not None:
for item in items:
self.add(item)
def clear(self):
"""
>>> bag = Bag(list("ABCDEB"))
>>> bag.count("A"), bag.count("B"), bag.count("Z")
(1, 2, 0)
>>> len(bag) # "Z" was added but count of 0 doesn't count in len
6
>>> bag.clear()
>>> len(bag)
0
"""
self.__bag.clear()
def add(self, item):
"""
>>> bag = Bag(list("ABCDEB"))
>>> bag.add("B")
>>> bag.add("X")
>>> bag.count("A"), bag.count("B"), bag.count("Z")
(1, 3, 0)
"""
self.__bag[item] = self.__bag.get(item, 0) + 1
def __delitem__(self, item):
"""
>>> bag = Bag(list("ABCDEB"))
>>> print(len(bag))
6
>>> del bag["B"]
>>> del bag["Z"]
Traceback (most recent call last):
...
KeyError: 'Z'
>>> bag.count("A"), bag.count("B"), bag.count("Z")
(1, 1, 0)
>>> del bag["B"]
>>> bag.count("A"), bag.count("B"), bag.count("Z")
(1, 0, 0)
>>> del bag["C"], bag["D"], bag["E"]
>>> print(len(bag))
1
>>> del bag["A"]
>>> print(len(bag))
0
>>> del bag["A"]
Traceback (most recent call last):
...
KeyError: 'A'
"""
if self.__bag.get(item) is not None:
self.__bag[item] -= 1
if self.__bag[item] <= 0:
del self.__bag[item]
else:
raise KeyError(str(item))
def count(self, item):
"""
>>> bag = Bag(list("ABCDEB"))
>>> bag.count("B"), bag.count("X")
(2, 0)
"""
return self.__bag.get(item, 0)
def __len__(self):
"""
>>> bag = Bag(list("ABCDEB"))
>>> len(bag)
6
>>> bag.add("B")
>>> len(bag)
7
>>> bag.add("X")
>>> len(bag)
8
>>> bag.add("B")
>>> len(bag)
9
>>> del bag["Z"]
Traceback (most recent call last):
...
KeyError: 'Z'
>>> len(bag)
9
>>> del bag["A"]
>>> len(bag)
8
>>> for _ in range(4): del bag["B"]
>>> len(bag)
4
>>> bag.clear()
>>> len(bag)
0
"""
return sum(count for count in self.__bag.values())
def __iter__(self):
"""
>>> bag = Bag(list("DABCDEBCCDD"))
>>> for key in sorted(bag.items()): print(key, end=" ")
A B B C C C D D D D E
>>> for key in sorted(bag): print(key, end=" ")
A B B C C C D D D D E
>>> for _ in range(4): del bag["D"]
>>> for key in sorted(bag.items()): print(key, end=" ")
A B B C C C E
>>> for key in sorted(bag): print(key, end=" ")
A B B C C C E
>>> del bag["A"]
>>> for key in sorted(bag.items()): print(key, end=" ")
B B C C C E
>>> for key in sorted(bag): print(key, end=" ")
B B C C C E
"""
return (item for item, count in self.__bag.items()
for _ in range(count))
items = __iter__
def __contains__(self, item):
"""
>>> bag = Bag(list("DABCDEBCCDD"))
>>> "D" in bag
True
>>> del bag["D"]
>>> "D" in bag
True
>>> del bag["D"]
>>> "D" in bag
True
>>> del bag["D"]
>>> "D" in bag
True
>>> del bag["D"]
>>> "D" in bag
False
>>> "X" in bag
False
"""
return item in self.__bag
if __name__ == "__main__":
import doctest
doctest.testmod()