Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN PYTHON ----------------------- class bidict(dict): def __init__(self, inital=[], **kargs): pass pts) Define a class named bidict (bidirectional dict) derived from the dict class, in addition

image text in transcribed

IN PYTHON

-----------------------

class bidict(dict): def __init__(self, inital=[], **kargs): pass

pts) 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 auxiliary/attribute dictionary that uses the bidict's values as keys, associated to a set of the bidict's keys (the 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 class attribute that keeps track of a list of all the objects created from this class, which two static functions 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 (al) has a hash attribute, and (a2) the attribute's value is not None; also (b) if the object is iterable (has an_iter attribute) then every value iterated over is also hashable (by this same definition). Also note that str is hashable as a special case: if we apply the above definition it will create infinite recursion because every value that we iterate over in a str is a str that we can iterate over! Raise a ValueBrror 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 you should write with the name is_hashable) Finally, rdict should never store a key that is associated with an empty set:setitemand delitemmust 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 bdict (a= [] ) then-init_ would raise a ValueError exception. We will continue using this example below pts) 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 auxiliary/attribute dictionary that uses the bidict's values as keys, associated to a set of the bidict's keys (the 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 class attribute that keeps track of a list of all the objects created from this class, which two static functions 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 (al) has a hash attribute, and (a2) the attribute's value is not None; also (b) if the object is iterable (has an_iter attribute) then every value iterated over is also hashable (by this same definition). Also note that str is hashable as a special case: if we apply the above definition it will create infinite recursion because every value that we iterate over in a str is a str that we can iterate over! Raise a ValueBrror 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 you should write with the name is_hashable) Finally, rdict should never store a key that is associated with an empty set:setitemand delitemmust 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 bdict (a= [] ) then-init_ would raise a ValueError exception. We will continue using this example below

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

Recommended Textbook for

Intelligent Information And Database Systems Third International Conference Achids 2011 Daegu Korea April 2011 Proceedings Part 2 Lnai 6592

Authors: Ngoc Thanh Nguyen ,Chong-Gun Kim ,Adam Janiak

2011th Edition

3642200419, 978-3642200410

More Books

Students also viewed these Databases questions