Question: ANSWER IN PYTHON. Fill in area where it says YOUR CODE HERE. Thank you Comparison and Logical Operators Comparisons If d is an AD, what

ANSWER IN PYTHON. Fill in area where it says YOUR CODE HERE.

ANSWER IN PYTHON. Fill in area where it says YOUR CODE HERE.Thank you Comparison and Logical Operators Comparisons If d is an AD,what is the meaning of d > 2? In numpy, when wehave an array a, and we write a > 2, we obtainThank you

Comparison and Logical Operators Comparisons If d is an AD, what is the meaning of d > 2? In numpy, when we have an array a, and we write a > 2, we obtain the array consisting of boolean entries, indicating whether the elements of a are greater than two or not: U import numpy as np a = np.array([2, 3, 4, 1, 2]) a > 2 array([False True True, False, False]) This suggests adopting a similar semantics for inequalities, defining the behavior of the comparison operators: 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) Let us see how this works: [] AD(cat=4, bird=2) > 2 {'bird': false, 'cat': True} Once we have a dictionary mapping from values to booleans, we need some way of testing whether all keys, or some keys, map to True. In Python, the all() function returns whether all elements in a boolean list are true: [] print(all([True, False, True, True])) print(any ( [True, True, True, True])) False True So we can define the all and any methods for an AD as follows: [] def ad_all(self): return all(self.values) AD.all = ad_all 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 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. U 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 Let us try this out. U d = AD (green=3, blue=2, red=1) d.filter (lambda x : x > 1) {'blue': 2, 'green': 3} [] d = AD (green=3, blue=2, red=1) (d > 1).filter() u (d > 1).all() False (d > 0).all() DTrue 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 codel. # YOUR CODE HERE INI

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!