Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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.
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.
Print the sum of the elements of set s on a single line.
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
4
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.
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)
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))
# 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"
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))
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