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.
Set .discard(), .remove() & .pop()
Set .discard(), .remove() & .pop()
Sort by
recency
|
1075 Discussions
|
Please Login in order to post a comment
Why would this not work:
import sys
def solution(n, values, lines, N): for i in range(N): if lines[i][0] == "d": values.discard(int(lines[i][-1])) elif lines[i][0] == "r": values.remove(int(lines[i][-1])) else: values.pop()
if name=="main": n = int(input()) values = set(map(int, input().split())) N = int(input())
This code works just fine..
n = int(input()) S = set(map(int,input().split())) N = int(input()) arr = [] for i in range(N): command = list(input().split()) if command[0] == 'pop': S.discard(min(S)) else: S.discard(int(command[1])) print(sum(S))
This question and solution needs to be updated as it depends on pop() removing a specific element. However, by definition the pop() method is used to remove and return an arbitrary element from a set. So in my case my solution results in 6 instead of the expected 4.
Here is HackerRank Set .discard(), .remove() & .pop() in python solution - https://programmingoneonone.com/hackerrank-set-discard-remove-pop-solution-in-python.html