Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Answer in Python. Fill in where it says YOUR CODE HERE. class AD(dict): def add_(self, other): return AD. _binary_op(self, other, lambda x, y: x +
Answer in Python. Fill in where it says YOUR CODE HERE.
class AD(dict): def add_(self, other): return AD. _binary_op(self, other, lambda x, y: x + y, 0) def sub (self, other): return AD._binary_op(self, other, lambda x, y: x - y, 0) @staticmethod def _binary_op(left, right, op, neutral): r = AD() 1_keys = set(left.keys()) if isinstance(left, dict) else set() r_keys = set(right.keys()) if isinstance(right, dict) else set(). for k in 1_keys | r_keys: # If the right (or left) element is a dictionary (or an AD), # we get the elements from the dictionary; else we use the right # or left value itself. This implements a sort of dictionary # broadcasting. 1_val = left.get(k, neutral) if isinstance (left, dict) else left r_val right.get(k, neutral) if isinstance (right, dict) else right r[k] op(l_val, r_val) return r = Implementing the Sock Drawer In Terms of Arithmetic Dictionaries ### Exercise: Implement the sock drawer class using ADs class ADSockDrawer(object): def _init__(self): self.drawer = AD() def add_sock (self, color): """Adds a sock of the given color to the drawer. Do it in one line of code. "!" # YOUR CODE HERE def get_pair(self, color): "" "Returns False if there is no pair of the given color, and True if there is. In the latter case, it removes the pair from the drawer. Do it in 5 lines of code or less.""" # YOUR CODE HERE @property def available_colors (self): "" "Lists the colors for which we have at least two socks available. Do it in 1 line of code."" # YOUR CODE HEREStep 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