In the following code user will enter single integer N as number of pairs and then N
pair as b
.
4
r 1
a 1
a 2
a 1
r
means remove associated number of pair and a
means add the associated number into list. Code should print(empty!) if the list is empty and if not print the median of list in each iteration. The correct output should be like this:
empty!
1
1.5
1
but my code outputs the result after each input that users enters, like below:
4
r 1
empty!
a 1
1
a 2
1.5
a 1
1
import statistics
N = int(input())
B=[]
for i in range(N):
b = (input().split())
action, value = b
if action == 'a':
B.append(int(value))
elif action == 'r':
try:
B.remove(int(value))
except ValueError:
pass
if len(B) == 0:
print('empty!')
else:
print(statistics.median(B))
Aucun commentaire:
Enregistrer un commentaire