Set .discard(), .remove() & .pop() Solution
3 min read

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

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

.remove(x)

This operation removes element  from the set.
If element  does not exist, it raises a KeyError.
The .remove(x) operation returns None.

Example>>> s = set([1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> s.remove(5) >>> print s set([1, 2, 3, 4, 6, 7, 8, 9]) >>> print s.remove(4) None >>> print s set([1, 2, 3, 6, 7, 8, 9]) >>> s.remove(0) KeyError: 0


.discard(x)

This operation also removes element  from the set.
If element  does not exist, it does not raise a KeyError.
The .discard(x) operation returns None.

Example>>> s = set([1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> s.discard(5) >>> print s set([1, 2, 3, 4, 6, 7, 8, 9]) >>> print s.discard(4) None >>> print s set([1, 2, 3, 6, 7, 8, 9]) >>> s.discard(0) >>> print s set([1, 2, 3, 6, 7, 8, 9])


.pop()

This operation removes and return an arbitrary element from the set.
If there are no elements to remove, it raises a KeyError.

Example>>> s = set([1]) >>> print s.pop() 1 >>> print s set([]) >>> print s.pop() KeyError: pop from an empty set


Task

You have a non-empty set , and you have to execute  commands given in  lines.

The commands will be pop, remove and discard.

Input Format

The first line contains integer , the number of elements in the set .
The second line contains  space separated elements of set . All of the elements are non-negative integers, less than or equal to 9.
The third line contains integer , the number of commands.
The next  lines contains either pop, remove and/or discard commands followed by their associated value.

Constraints


Output Format

Print the sum of the elements of set  on a single line.

Sample Input

9
1 2 3 4 5 6 7 8 9
10
pop
remove 9
discard 9
discard 8
remove 7
pop 
discard 6
remove 5
pop 
discard 5

Sample Output

4

Explanation

After completing these  operations on the set, we get set([4]). Hence, the sum is .

Note: Convert the elements of set s to integers while you are assigning them. To ensure the proper input of the set, we have added the first two lines of code to the editor.

Solution in Python

n = int(input())
s = set(map(int, input().split()))
d = {"pop":s.pop, "remove":s.remove, "discard": s.discard}
for _ in range(int(input())):
    c = input().split()
    d[c[0]](int(c[1])) if len(c)>1 else d[c[0]]()
print(sum(s))

Explanation

What I actually did is bind those 3 words to their respective functions for the list s and then store it in a dictionary.

Now these values will become equal

>>> d["pop"]==s.pop
True
>>> d["remove"]==s.remove
True
>>> d["discard"]==s.discard
True

If a user enters

remove 5

Both of the following will perform our desired action

d["remove"](int("5"))
s.remove(5)

So we take input from the user and perform the action using d instead of s

>>> c = input().split()
["remove", "5"]
>>> c[0] == "remove"
True
>>> c[1] == "5
True

When c[1] is blank len of c will be 1 in such case we call our function without any parameter. Hence our code can be written like this

d[c[0]](int(c[1])) if len(c)>1 else d[c[0]]()

Or this

if len(c)>1:
	d[c[0]](int(c[1])) 
else:
	d[c[0]]()

And that's it! Hope it helped you understand clearly.

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×