With respect to your code .add is an invalid syntax with a list, .append should be used here, and before removing we have to check whether the val is already present in the list. If it is present, then only we should remove it.
#!/bin/python3
import math
import os
import random
import re
import sys
class Multiset:
def __init__(self):
self.items = []
def add(self, val):
#adds one occurrence of val from the multiset, if any
return self.items.append(val)
pass
def remove(self, val):
# removes one occurrence of val from the multiset, if any
if self.items.count(val) != 0:
return self.items.remove(val)
pass
def __contains__(self, val):
# returns True when val is in the multiset, else returns False
return val in self.items
def __len__(self):
# returns the number of elements in the multiset
return len(self.items)
if __name__ == '__main__':
def performOperations(operations):
m = Multiset()
result = []
for op_str in operations:
elems = op_str.split()
if elems[0] == 'size':
result.append(len(m))
else:
op, val = elems[0], int(elems[1])
if op == 'query':
result.append(val in m)
elif op == 'add':
m.add(val)
elif op == 'remove':
m.remove(val)
return result
q = int(input())
operations = []
for _ in range(q):
operations.append(input())
result = performOperations(operations)
fptr = open(os.environ['OUTPUT_PATH'], 'w')
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()
Answer from Champion_pro on Stack OverflowGitHub
github.com › sanskritilakhmani › Hackerrank › blob › main › Certification_Test_Python › Basic › Multiset_Implementation
Hackerrank/Certification_Test_Python/Basic/Multiset_Implementation at main · sanskritilakhmani/Hackerrank
#!/bin/python3 · · import math · import os · import random · import re · import sys · · · class Multiset: def __init__(self): self.items=[] def add(self, val): # adds one occurrence of val from the multiset, if any · self.items.append(val) pass ·
Author sanskritilakhmani
GitHub
github.com › MD-MAFUJUL-HASAN › HackerRank-Python-Basic-Skills-Certification-Test
GitHub - MD-MAFUJUL-HASAN/HackerRank-Python-Basic-Skills-Certification-Test: These Contain Basic Skills Certification Test Solution of Python programming language in HackerRank😏
These Contain Basic Skills Certification Test Solution of Python programming language in HackerRank😏 - MD-MAFUJUL-HASAN/HackerRank-Python-Basic-Skills-Certification-Test
Starred by 14 users
Forked by 6 users
Languages Python
GitHub
github.com › sanskritilakhmani › Hackerrank
GitHub - sanskritilakhmani/Hackerrank: Solutions to the practice exercises, coding challenges, and other problems on Hackerrank!
Certificates · Python · XML · Java · SQL · Day 0 · Mean-Median-Mode · Weighted-Mean · Day_1 · Interquartile-Range · Quartiles · Standard-Deviation · Day_2 · Probability · Coumpound_Event · More_Dice · Hello_World · Data-Types · Operators · Conditional-Statements · Class-vs-Instance · Loops · Review · Array · Dictionaries · Recursion · Binary-Number · Python · Multiset-Implementation ·
Starred by 24 users
Forked by 21 users
Languages Python 59.3% | C 38.5% | Java 2.2% | Python 59.3% | C 38.5% | Java 2.2%
YouTube
youtube.com › watch
Python (Basic) Certification 3 [ MultiSet Implementation ] | Hackerrank Certifications - YouTube
Thanks if u r watching us ... #Dev19 #C #Python #Dev19 #HackerankSolutions #C #C++ #Java #PythonPlease Subscribe Us ....
Published June 30, 2020
YouTube
youtube.com › codelink
hackerrank python basic certification solutions multiset implementation - YouTube
Download this code from https://codegive.com Title: Implementing Multiset in Python for HackerRank Python Basic CertificationIntroduction:HackerRank is a pop...
Published December 27, 2023 Views 69
GitHub
github.com › connectaditya › HackerRank_solution
GitHub - connectaditya/HackerRank_solution: This repository contains the most efficient hackerrank solutions for most of the hackerrank challenges and Domains. · GitHub
This repository contains the most efficient hackerrank solutions for most of the hackerrank challenges and Domains. HackerRank Python Programming Solutions
Starred by 4 users
Forked by 4 users
Languages Python
Top answer 1 of 4
6
With respect to your code .add is an invalid syntax with a list, .append should be used here, and before removing we have to check whether the val is already present in the list. If it is present, then only we should remove it.
#!/bin/python3
import math
import os
import random
import re
import sys
class Multiset:
def __init__(self):
self.items = []
def add(self, val):
#adds one occurrence of val from the multiset, if any
return self.items.append(val)
pass
def remove(self, val):
# removes one occurrence of val from the multiset, if any
if self.items.count(val) != 0:
return self.items.remove(val)
pass
def __contains__(self, val):
# returns True when val is in the multiset, else returns False
return val in self.items
def __len__(self):
# returns the number of elements in the multiset
return len(self.items)
if __name__ == '__main__':
def performOperations(operations):
m = Multiset()
result = []
for op_str in operations:
elems = op_str.split()
if elems[0] == 'size':
result.append(len(m))
else:
op, val = elems[0], int(elems[1])
if op == 'query':
result.append(val in m)
elif op == 'add':
m.add(val)
elif op == 'remove':
m.remove(val)
return result
q = int(input())
operations = []
for _ in range(q):
operations.append(input())
result = performOperations(operations)
fptr = open(os.environ['OUTPUT_PATH'], 'w')
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()
2 of 4
1
class Multiset:
def __init__(self):
self.l = []
def add(self, val):
# adds one occurrence of val from the multiset, if any
pass #('pass' is a nothing operation. When it execute, nothing happens.)
return self.l.append(val)
def remove(self, val):
# removes one occurrence of val from the multiset, if any
pass
if val in self.l:
return self.l.remove(val)
def __contains__(self, val):
# returns True when val is in the multiset, else returns False
if val in self.l:
return True
else:
return False
def __len__(self):
# returns the number of elements in the multiset
return len(self.l)
if __name__ == '__main__':
Medium
chase2learn.medium.com › hackerrank-python-programming-solutions-7bdacca4cbc2
HackerRank Python Programming Solutions | by chase2learn.com | Medium
June 25, 2021 - python (basic) skills certification test hackerrank solution | hackerrank python (basic certification solutions) | hackerrank python certification solutions | python multiset implementation hackerrank solution | python get additional info | hackerrank solution | hackerrank python solution if-else | hackerrank solutions python 30 days of code | hackerrank python solutions loops
YouTube
youtube.com › being bozon
Hackerrank Python Certification Solutions | Multiset class in python - YouTube
This video provides video solutions to the Hacker rank Python Certification .This video contains the solution to the Multi set Problem of the quiz.
Published August 19, 2020 Views 17K
YouTube
youtube.com › competitive panda
Hakerrank Python Certification Solutions [Hackerrank Certifications Solutions] - YouTube
Hackerrank Python certification solutions for Multiset Implementation and Shape classes with area method.Get certified with Hakerrank Python basic certifica...
Published May 5, 2021 Views 2K
Edwardtheharris
edwardtheharris.github.io › hacker-rank › python › python-basic-cert › implement_multiset › index.html
Multiset Implementation — Hacker Rank Challenges 0.0.1 documentation
Implement a multiset data structure using a Python object.
GitHub
github.com › MD-MAFUJUL-HASAN › HackerRank-Python-Basic-Skills-Certification-Test › blob › main › Multiset Implementation.py
HackerRank-Python-Basic-Skills-Certification-Test/Multiset Implementation.py at main · MD-MAFUJUL-HASAN/HackerRank-Python-Basic-Skills-Certification-Test
These Contain Basic Skills Certification Test Solution of Python programming language in HackerRank😏 - HackerRank-Python-Basic-Skills-Certification-Test/Multiset Implementation.py at main · MD-MAFUJUL-HASAN/HackerRank-Python-Basic-Skills-Certification-Test
Author MD-MAFUJUL-HASAN
YouTube
youtube.com › watch
Python (Basic) Certification | Hackerrank Certifications - YouTube
Thanks if u r watching us ... #Dev19 #C #Python #Dev19 #HackerankSolutions #C #C++ #Java #PythonPlease Subscribe Us ....
Published June 15, 2020
GitHub
github.com › ShahnawazKakarh › hacker-rank-python-basics
GitHub - ShahnawazKakarh/hacker-rank-python-basics: This repository contains my solutions for the HackerRank Python Basics practice problems and tests. The goal is to document my approach, maintain clean and efficient code, and make it easy to re-run or review later.
Each .py file corresponds to a specific HackerRank problem. File names are descriptive for easy identification. avg_function.py # Create a function that returns the average of given numbers · vending_machine.py # Simulate a vending machine transaction process · multiset_implementation.py # Multiset implementation (add,remove,contain,len) Basic operations
Author ShahnawazKakarh
GitHub
github.com › topics › hackerrank-solutions
hackerrank-solutions · GitHub Topics · GitHub
shell solutions hackerrank hackerrank-python hackerrank-solutions hackerrank-algorithms-solutions hackerrank-python-solutions hackerrank-challenges ... A collection of solutions to competitive programming exercises on HackerRank. competitive-programming hackerrank hackerrank-solutions hackerrank-certificates hackerrank-certification
GitHub
github.com › mntushar › hackerrank-certificate-test-python › blob › master › Multiset Implementation.py
hackerrank-certificate-test-python/Multiset Implementation.py at master · mntushar/hackerrank-certificate-test-python
#!/bin/python · · import math · import os · import random · import re · import sys · · · class Multiset: def __init__(self): self.data = [] · · def add(self, val): # adds one occurrence of val from the multiset, if any ·
Author mntushar