Question: Please answer in Python, the only parts that needs answering is in the LAST PICTURE after class ADSockDrawer(object) : under every instance of '# YOUR

Please answer in Python, the only parts that needs answering is in the LAST PICTURE after class ADSockDrawer(object): under every instance of '# YOUR CODE HERE.' All other pictures are just to supply information.

Please answer in Python, the only parts that needs answering is in

the LAST PICTURE after class ADSockDrawer(object): under every instance of '# YOUR

CODE HERE.' All other pictures are just to supply information. Should only

have 1 line of code for add_sock, ~5 lines of code for

get_pair, and then 1 last line of code for available_colors. Thank youso much! Will upvote right answer 3 1 class AD (dict): 2

Should only have 1 line of code for add_sock, ~5 lines of code for get_pair, and then 1 last line of code for available_colors. Thank you so much! Will upvote right answer

3 1 class AD (dict): 2 def _add_(self, other): 4 | return AD._binary_op(self, other, lambda x, y: x + y, e) 5 def sub_(self, other): 7 return AD._binary_op(self, other, lambda x, y: x - y, ) 6 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 @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(1_val, r_val) return r In the implementation above, we have factored together what would have been the common part of the implementation of + and in the method _binary_op. The _binary op method is a static method: it does not refer to a specific object. In this way, we can decide separately what to provide to it as the left and right hand sides, this will be useful later, as we shall see. To call it, we have to refer to it via AD._binary op rather than self._binary_op, as would be the case for a normal (object) method. Furthermore, in the _binary op method, we do not have access to self, since self refers to a specific object, while the method is generic, defined for the class. The _binary op method combines values from its left and right operands using operator op, and the default neutral if a value is not found. The two operands left and right are interpreted as dictionaries if they are a subclass of dict, and are interpreted as constants otherwise. We have defined our class AD as a subclass of the dictionary class dict. This enables us to apply to an AD all the methods that are defined for a dictionary, which is really quite handy. [23] ### Exercise: Implement multiplication and division def ad_mul(self, other): # YOUR CODE HERE return AD. _binary op(self, other, lambda x, y: x* y, 1) AD. mul = ad_mul # Define below division, similarly. # YOUR CODE HERE def ad_truediv(self, other): return AD._binary op(self, other, lambda x, y: float(x) / float(y), 1) AD._truediv_ = ad_truediv # And finally, define below INTEGER division. # YOUR CODE HERE def ad_floordiv(self, other): return AD. _binary op(self, other, lambda x, y: x // y, 1) AD. _floordiv_ = ad_floordiv [27] ### Exercise: Implement "max_items 11 IT IT def ad_max_items(self): """Returns a pair consisting of the max value in the AD, and of the set of keys that attain that value. This can be done in 7 lines of code.' # YOUR CODE HERE 0 x x = set() max = = = for key, value in self.items(): if value > max: x = set() X.add(key) max = value elif value == max: x.add(key) if max == 0: return None, set() return max, x # Remember the use of the property decorator. AD.max_items = property(ad_max_items) [30] def ad_lt(self, other): return AD. _binary_op(self, other, lambda x, y: x y, 0) = lambda 1, r: AD._binary_op(1, r, lambda x, y: x = y, 0) AD [34] def ad_all(self): return all(self.values) AD.all = ad all [35] AD.all = lambda self : all(self.values()) AD.any = lambda self : any(self.values()) We also add a method for filtering the elements that satisfy a condition. If the condition is left unspecified, we use the truth-value of the elements themselves. [36] def ad_filter(self, f=None): return AD({k: v for k, v in self.items() if (f(v) if f is not None else v)}) AD.filter = ad_filter [41] ### 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 HERE

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!