Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

Set .discard(), .remove() & .pop() Hacker Rank Solution – Queslers

Problem: Set .discard(), .remove() & .pop() Hacker Rank Solution

Task :

You have a non-empty set s, and you have to execute N commands given in N lines.
The commands will be pop, remove and discard.

Input Format :

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

Constraints :

  • 0 < n < 20
  • 0 < N < 20

Output Format :

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

Sample Input :

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

Sample Output :


Explanation :

After completing these 10 operations on the set, we get set([4]). Hence, the sum is 4.
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() & .pop() Hacker Rank Solution in python 2

n = int(raw_input())
numbers = set()
for x in (raw_input().split(' ')):
for x in range(int(raw_input())):
  cmd = raw_input().split(' ')
  if cmd[0] == 'pop':
  elif cmd[0] == 'remove':
print sum(numbers)

Set .discard(), .remove() & .pop() Hacker Rank Solution in python 3

num = int(input())
data = set(map(int, input().split()))
operations = int(input())

for x in range(operations):
  oper = input().split()
  if oper[0] == "remove":
  elif oper[0] == "discard":

Set .discard(), .remove() & .pop() Hacker Rank Solution in pypy

# Enter your code here. Read input from STDIN. Print output to STDOUT
n = int(raw_input())
s = set(raw_input().split())
m = int(raw_input())
for i in range (0, m):
    lis = raw_input().split()
    # s,type(s)
    #print lis
    if lis[0] == 'remove':
    if lis[0] == 'discard':
    if lis[0] == 'pop':
        s = set(sorted(s, reverse = True))
        s = set(sorted(s, reverse = True))

if  len(s) != 0:
    print   sum(map(int, s))
    print "0"

Set .discard(), .remove() & .pop() Hacker Rank Solution in pypy 3

import pdb
m = int(input())
lst=[int(j) for j in input().strip().split()]
n= int(input())
for i in range(n):
    command = input().strip().split()
    if len(command) ==1 :
        methodToCall = getattr(st, command[0])
        commd, *args= [command[0], int(command[1])]
        getattr(st, commd)(*args)
Set .discard(), .remove() & .pop() Hacker Rank Solution Review:

In our experience, we suggest you solve this Set .discard(), .remove() & .pop() Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Set .discard(), .remove() & .pop() problem is available on Hacker Rank for Free, if you are stuck anywhere between a compilation, just visit Queslers to get Set .discard(), .remove() & .pop() Hacker Rank Solution


I hope this Set .discard(), .remove() & .pop() Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning Programming in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Set .add() Hacker Rank Solution

itertools.combinations_with_replacement() Hacker Rank Solution

Word Order Hacker Rank Solution

Staircase Hacker Rank Solution

A Very Big Sum Hacker Rank Solution

Leave a Reply

Your email address will not be published. Required fields are marked *