-2

I was trying to solve some of hackerrank questions where I got one question related to multiset. I tried this code but I little bit confused about at which point am I making mistake?

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)
    

    def remove(self, val):
        # removes one occurrence of val from the multiset, if any
        if len(self.items):
            if val in self.items:
                self.items.remove(val)

    def __contains__(self, val):
        if val in self.items:
            return True
        return False
        
    
    def __len__(self):
        # returns the number of elements in the multiset
        return len(self.items)

if __name__ == '__main__':

Sharma Shivlal
  • 21
  • 1
  • 1
  • 7

4 Answers4

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()
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__':
  • 2
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Adrian Mole Jul 24 '20 at 21:41
0

You have used dictionary. try using list. Try using this code

class Multiset:
    def __init__(self):
        self.itmes = []


    def add(self, val):
        # adds one occurrence of val from the multiset, if any
        return self.items.add(val)

    def remove(self, val):
        # removes one occurrence of val from the multiset, if any
        return self.items.remove(val)

    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.itmes)
if __name__ == '__main__':
-2

Try this one.

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)

    def remove(self, val):
        # removes one occurrence of val from the multiset, if any
        if val in self.items:
            return self.items.remove(val)

    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__':
  • Please add some prose – Mad Physicist Jun 21 '20 at 03:56
  • Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – rizerphe Jun 21 '20 at 05:26