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 :

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 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(' ')):
  numbers.add(int(x))
for x in range(int(raw_input())):
  cmd = raw_input().split(' ')
  if cmd[0] == 'pop':
    numbers.pop()
  elif cmd[0] == 'remove':
      numbers.remove(int(cmd[1]))
  else:
    numbers.discard(int(cmd[1]))
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":
    data.remove(int(oper[1]))
  elif oper[0] == "discard":
    data.discard(int(oper[1]))
  else:
    data.pop()
    
print(sum(data))

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':
        s.remove(lis[1])
    if lis[0] == 'discard':
        s.discard(lis[1])
    if lis[0] == 'pop':
        s = set(sorted(s, reverse = True))
        s.pop()
        s = set(sorted(s, reverse = True))

if  len(s) != 0:
    print   sum(map(int, s))
else:
    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()]
lst.reverse()
st=set(lst)
n= int(input())
for i in range(n):
    command = input().strip().split()
    #pdb.set_trace()
    if len(command) ==1 :
        methodToCall = getattr(st, command[0])
        methodToCall()
        #print(st)   
    else:
        commd, *args= [command[0], int(command[1])]
        getattr(st, commd)(*args)
        #print(st)   
print(sum(st))
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

Conclusion:

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.