Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Set .discard(), .remove() & .pop() solution in python

YASH PAL, 31 July 2024

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.

HackerRank Set .discard(), .remove() & .pop() solution in python

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

coding problems solutions Python Solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes