We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def fun(func, num):
if func == "remove" and num in set1:
set1.remove(num)
elif func == "discard":
set1.discard(num)
/////////////////////////////////////
size = int(input())
set1 = set(map(int, input().split())) # Convert elements to integers
commands = int(input())
for i in range(commands):
text = input().split()
func = text[0]
if func == "pop":
if set1: # Check if the set is not empty before popping
set1.pop()
elif func == "remove" or func == "discard":
num = int(text[1])
fun(func, num)
print(sum(set1))
My code outputs 6 for the given test-case, what should I do?
How do people post their code in the systematic order here??
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Set .discard(), .remove() & .pop()
You are viewing a single comment's thread. Return to all comments →
def fun(func, num): if func == "remove" and num in set1: set1.remove(num) elif func == "discard": set1.discard(num) ///////////////////////////////////// size = int(input()) set1 = set(map(int, input().split())) # Convert elements to integers commands = int(input())
for i in range(commands): text = input().split() func = text[0] if func == "pop": if set1: # Check if the set is not empty before popping set1.pop() elif func == "remove" or func == "discard": num = int(text[1]) fun(func, num)
print(sum(set1))
My code outputs 6 for the given test-case, what should I do?
How do people post their code in the systematic order here??