-
Notifications
You must be signed in to change notification settings - Fork 1
/
MultiSet_Python.txt
53 lines (42 loc) · 1.3 KB
/
MultiSet_Python.txt
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
#Problem - Implement Multiset in Python.
class Multiset:
def __init__(self):
self.M=[]
def add(self, val):
self.M.append(val)
def remove(self, val):
if len(self.M):
if val in self.M:
self.M.remove(val)
def __contains__(self, val):
if val in self.M:
return True
return False
def __len__(self):
return len(self.M)
if __name__ == '__main__':
def performOperations(operations):
m = Multiset()
result = []
for op_str in operations:
elems = op_str.split()
if elems[0] == 'size':
result.append(len(m))
else:
op, val = elems[0], int(elems[1])
if op == 'query':
result.append(val in m)
elif op == 'add':
m.add(val)
elif op == 'remove':
m.remove(val)
return result
q = int(input())
operations = []
for _ in range(q):
operations.append(input())
result = performOperations(operations)
fptr = open(os.environ['OUTPUT_PATH'], 'w')
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()