Question: Please use Python! ----------- class SubtractionDict(dict): a SubtractionDict includes all properties and methods of the dict class. Additionally, implements dictionary subtraction via the -

Please use Python!
-----------
class SubtractionDict(dict): """ a SubtractionDict includes all properties and methods of the dict class. Additionally, implements dictionary subtraction via the - binary operator. If SD1 and SD2 are both SubtractionDicts whose values are numbers (ints or floats), and if all values in SD1 are equal to or larger than their corresponding values in SD2, then SD1 - SD2 is a new SubtractionDict whose keys are the keys of SD1 and whose values are the difference in values between SD1 and SD2. Keys present in SD1 but not in SD2 are handled as though they are present in SD2 with value 0. A ValueError is raised when: 1. SD2 contains keys not contained in SD1. 2. The result of subtraction would result in negative values. If subtraction would result in a value of exactly zero, the key is instead removed from the result. Examples: # making strawberry-onion pie SD1 = SubtractionDict({"onions" : 3, "strawberries (lbs)" : 2}) SD2 = SubtractionDict({"onions" : 1, "strawberries (lbs)" : 1}) SD1 - SD2 # == SubtractionDict({"onions" : 2, "strawberries (lbs)" : 1}) # raises error SD1 = SubtractionDict({"onions" : 3, "strawberries (lbs)" : 2}) SD2 = SubtractionDict({"onions" : 4, "strawberries (lbs)" : 1}) SD1 - SD2 # error # raises error SD1 = SubtractionDict({"onions" : 3, "strawberries (lbs)" : 2}) SD2 = SubtractionDict({"onions" : 1, "snozzberries (lbs)" : 1}) SD1 - SD2 # error # key removed SD1 = SubtractionDict({"onions" : 3, "strawberries (lbs)" : 2}) SD2 = SubtractionDict({"onions" : 1, "strawberries (lbs)" : 2}) SD1 - SD2 # == SubtractionDict({"onions" : 2}) """ # your code here
-------------
We also need to check the 4 examples.
Please use Python!
Part B (20 points) Write a class that matches the following docstring. You may find it helpful to consult the lecture notes in which we first defined ArithmeticDicto. Then, demonstrate each of the examples given in the docstring. In [ ]: class SubtractionDict(dict): a SubtractionDict includes all properties and methods of the dict class. Additionally, implements dictionary subtraction via the - binary operator. If SD1 and SD2 are both SubtractionDicts whose values are numbers (ints or floats), and if all values in SD1 are equal to or larger than their corresponding values in SD2, then SD1 - SD2 is a new SubtractionDict whose keys are the keys of SD1 and whose values are the difference in values between SD1 and SD2. Keys present in SD1 but not in SD2 are handled as though they are present in SD2 with value 0. A ValueError is raised when: 1. SD2 contains keys not contained in SD1. 2. The result of subtraction would result in negative values. If subtraction would result in a value of exactly zero, the key is instead removed from the result. Examples: # making strawberry-onion pie SD1 = SubtractionDict( {"onions": 3, "strawberries (lbs)" : 2}) SD2 = SubtractionDict( {"onions" : 1, "strawberries (lbs)" : 1}) SD1 - SD2 # == SubtractionDict( {"onions": 2, "strawberries (lbs) : 1}) # raises error SD1 = SubtractionDict(["onions": 3, "strawberries (lbs) : 2}) SD2 = SubtractionDict( {"onions": 4, "strawberries (lbs)" : 1}) SD1 SD2 # error # raises error SD1 = SubtractionDict( {"onions": 3, "strawberries (lbs) : 2}) SD2 = SubtractionDict( {"onions" : 1, snozzberries (lbs) : 1}) SD1 - SD2 # error # key removed SD1 = SubtractionDict({"onions": 3, "strawberries (lbs) : 2}) SD2 = SubtractionDict( {"onions" : 1, "strawberries (lbs) : 2}) SD1 SD2 # == SubtractionDict("onions" : 2}) 000 # your code here
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
