Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Define a class named bidict ( bi directional dict ) derived from the dict class; in addition to being a regular dictionary (using inheritance), it

Define a class named bidict (bidirectional dict) derived from the dict class; in addition to being a regular dictionary (using inheritance), it also defines an auxillary/attribute dictionary that uses the bidicts values as keys, associated to a set of the bidicts keys (they keys associated with that value). Remember that multiple keys can associate to the same value, which is why we use a set: since keys are hashable (hashable = immutable) we can store them in sets. Finally, the bidict class stores a list of all the objects created from this class, which two static function manipulate.

Define the class bidict with the following methods (some override dict methods); you may also add helper methods: preface them with single underscores):

__init__ (self,initial =[],**kargs): initializes the dict in the base class and also creates an auxiliary dictionary (I used a defaultdict) named _rdict (reversedict: you must use this name for the bsc to work correctly) whose key(s) (the values in the bidict) are associated with a set of values (their keys in the bidict): initialize _rdict by iterating through the newly initialized dictionary.

For a bidict to work correctly, its keys and their associated values must all be hashable. We define any object as hashable if it (a1) has a __hash__ attribute, and (a2) the attributes value is not None; also (b) if the object is iterable (has an __iter__ attribute) then every value iterated over is also hashable. Raise a ValueError exception if any value is not hashable. Note you can use the hasattr and getattr functions (which I used in a recursive static helper method named hashable).

Finally, _rdict should never store a key that is associated with an empty set: __setitem__ and __delitem__ must ensure this invariant property (I wrote a helper method to help them do it).

For example if we define, bd = bidict(a=1,b=2,c=1) then _rdict stores {1: {'a', 'c'}, 2: {'b'}}. If I tried to construct bidict(a=[]) then __init__ would raise a ValueError exception. We will continue using this example below.

__setitem__ (self,key,value): set key to associate with value in the dictionary and modify the _rdict to reflect the change. For example, executing bd['a'] = 2 would result in the _rdict being changed to {1: {'c'}, 2: {'a', 'b'}}; then executing bd['c'] = 2 would result in the _rdict being changed to {2: {'a', 'b', 'c'}}, with 1 no longer being a key in _rdict because it is no longer a value in the bidict.

__delitem__ (self,key): for any key in the dictionary, remove and modify the _rdict to reflect the change.

__call__ (self,value): returns the set of keys (keys in the bidict) that associate with value: just lookup this information in the _rdict.

clear (self): remove all keys (and their associated values) from the bidict modify the _rdict to reflect the change..

all_objects (): a static method that returns a list of all the objects every created by bidict.

forget (object): a static method that forgets the specified bidict object, so all_objects doesnt return it

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

What do you think of the MBO program developed by Drucker?

Answered: 1 week ago