Sponsor

HackerRank Set .discard(), .remove() & .pop() solution in python | python Question 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.

In this Set .discard() .remove() & .pop() problem we need to develop a python program that can read an integer, and string separated with line and then we need to perform all the operations that given in the input and then we need to print the output on the screen.

Problem solution in Python 2 programming.

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)

Problem solution in Python 3 programming.

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))


Problem solution in pypy programming.

# 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"


Problem solution in pypy3 programming.

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))






Post a Comment

0 Comments