Question
Assignment 7, due Monday Nov. 19 at 10:00am Write an Object Oriented Python program using INHERITANCE from the class dict similar but richer in functionality
Assignment 7, due Monday Nov. 19 at 10:00am
Write an Object Oriented Python program using INHERITANCE from the class "dict" similar but richer in functionality than the Python program at:
class mdict : def __init__(self) : self.d = dict() def add(self,k,v) : vs=self.d.get(k) if vs is None : vs = set() vs.add(v) self.d[k]=vs def memb(self,k,v) : vs = self.d.get(k) if not vs or not v in vs : return False else : return True def __getitem__(self,k) : return self.d[k] def __contains__(self,k) : return k in self.d def __iter__(self) : for m in self.d : yield m,self.d[m] def __repr__(self) : return self.d.__repr__() ''' #usage:
>>> m=mdict() >>> m.add('a',1) >>> m.add('a',2) >>> m.add('b',1) >>> m.add('b',3) >>> m.add('c',0) >>> m {'a': {1, 2}, 'b': {1, 3}, 'c': {0}} >>> 2 in m False >>> 'a' in m True >>> for x in m : print(x) ... ('a', {1, 2}) ('b', {1, 3}) ('c', {0}) >>> m['b'] {1, 3} '''
The main goal of your class is to allow a set of values to be associated to a key, rather than a single value.
The class should support the usual "for x in" and "x in" operations, like "mdict" does.
Besides the methods exposed by mdict, you should also add an methods to:
1. delete a key and all its values
2. delete a specific value from the set of values associated to a key
3. clear the dictionary of all its contents
4. iterate over all key-value pairs contained in the dictionary
We do not specify the exact names and arguments of the methods in the API, that is left to your creativity.
5. A test set containing 3 examples of use for each method should also be provided.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started