Created
May 27, 2021 05:54
-
-
Save ryo1kato/1ca0058322628b5a34ac1554281588f7 to your computer and use it in GitHub Desktop.
Python Multiset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections | |
class Multiset(collections.Counter): | |
def add(self, elem): | |
self[elem] += 1 | |
def remove(self, elem): | |
self[elem] -= 1 | |
if self[elem] == 0: | |
del self[elem] | |
def pop(self): | |
k, v = self.popitem() | |
if v > 0: | |
self[k] = v-1 | |
return k |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment