Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedThank 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 1 Lnai 9284

Authors: Annalisa Appice ,Pedro Pereira Rodrigues ,Vitor Santos Costa ,Carlos Soares ,Joao Gama ,Alipio Jorge

1st Edition

3319235273, 978-3319235271

More Books

Students also viewed these Databases questions