Question: Write a class that matches the following docstring. Then, demonstrate each of the examples given in the docstring. class SubtractionDict(dict): a SubtractionDict includes all

Write a class that matches the following docstring. Then, demonstrate each of the examples given in the docstring.

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})

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!