Problem Statement (HackerRank)
You have a list of integers, initially the list is empty.
You have to process Q operations of three kinds:
add s: Add integer s to your list, note that an integer can exist more than one time in the list
del s: Delete one copy of integer s from the list, it's guaranteed that at least one copy of s will exist in the list.
cnt s: Count how many integers a are there in the list such that a AND s = a, where AND is bitwise AND operator
Input Format
First line contains an integer Q. Each of the following Q lines contains an operation type string T and an integer s.
Constraints
- \$1 \le Q \le 200000\$
- \$0 \le s \lt 2^{16}\$
Output Format
For each cnt s operation, output the answer in a new line.
Sample Input
7 add 11 cnt 15 add 4 add 0 cnt 6 del 4 cnt 15
Sample Output
1 2 2
Explanation
For first line, we have 15 AND 11 = 11 so the answer is 1
For second line, 6 AND 0 = 0 and 6 AND 4 = 4 so the answer is 2
For third line, 4 has been deleted and we have 15 AND 11 = 11 and 15 AND 0 = 0 so the answer is 2
My working code:
operations = int(raw_input())
current = 1
lst = []
while current <= operations:
count = 0
input_ = raw_input().split()
operation = input_[0]
num = int(input_[1])
if operation == 'add':
lst.append(num)
elif operation == 'cnt':
for number in lst:
if number & num == number:
count += 1
print(count)
elif operation == 'del':
lst.remove(num)
current += 1
list.count
but it doesn't serve a purpose as we have to count only those numbers which satisfy the given condition. \$\endgroup\$