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
After completing these 10 operations on the set, we get set(). 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 == 'pop': numbers.pop() elif cmd == 'remove': numbers.remove(int(cmd)) else: numbers.discard(int(cmd)) 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 == "remove": data.remove(int(oper)) elif oper == "discard": data.discard(int(oper)) 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 == 'remove': s.remove(lis) if lis == 'discard': s.discard(lis) if lis == '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) methodToCall() #print(st) else: commd, *args= [command, int(command)] 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.
More Hacker Rank Problem & Solutions >>