Set .discard(), .remove() & .pop()

  • + 0 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??