Hello Programmers/Coders, Today we are going to share solutions of Programming problems of HackerRank of Programming Language Python. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for Set discard() remove() and pop() in Python-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
Introduction To Python
Python is a widely-used, interpreted, object-oriented, and high-level programming language with dynamic semantics, used for general-purpose programming. It was created by Guido van Rossum, and first released on February 20, 1991.
Python is a computer programming language often used to build websites and software, automate tasks, and conduct data analysis. It is also used to create various machine learning algorithm, and helps in Artificial Intelligence. Python is a general purpose language, meaning it can be used to create a variety of different programs and isn’t specialized for any specific problems. This versatility, along with its beginner-friendliness, has made it one of the most-used programming languages today. A survey conducted by industry analyst firm RedMonk found that it was the most popular programming language among developers in 2020.
Link for the Problem – Set discard() remove() and pop() in Python – HackerRank Solution
Set discard() remove() and pop() in Python – HackerRank 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. 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.
Set discard() remove() and pop() in Python – HackerRank Solution
# Set discard() remove() and pop() in Python - Hacker Rank Solution # Python 3 # Set discard() remove() and pop() in Python - Hacker Rank Solution START n = int(input()) s = set(map(int,input().split())) num = int(input()) for i in range(num): ip = input().split() if ip[0]=="remove": s.remove(int(ip[1])) elif ip[0]=="discard": s.discard(int(ip[1])) else : s.pop() print(sum(list(s))) # Set discard() remove() and pop() in Python - Hacker Rank Solution END