Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In Python Please; It Would Mean A Lot CODE PROVIDED: class AD(dict): def __init__(self, *args, **kwargs): This initializer simply passes all arguments to dict, so

In Python Please; It Would Mean A Lot

CODE PROVIDED:

image text in transcribed

class AD(dict):

def __init__(self, *args, **kwargs):

"""This initializer simply passes all arguments to dict, so that

we can create an AD with the same ease with which we can create

a dict. There is no need, indeed, to repeat the initializer,

but we leave it here in case we want to create attributes specific

to an AD later."""

super().__init__(*args, **kwargs)

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

l_keys = set(left.keys()) if isinstance(left, dict) else set()

r_keys = set(right.keys()) if isinstance(right, dict) else set()

for k in l_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.

l_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

class Pantry(AD):

def __init__(self, ingredients):

"""We initialize the Pantry class by passing the

ingredients that were passed in to the initializer

for the superclass, AD"""

super().__init__(ingredients)

class Recipe:

def __init__(self, name, ingredients):

"""We initialize the Recipe class, which stores the

name of a recipe as well as the needed ingredients

in the form of an AD"""

self.name = name

self.ingredients = AD(ingredients)

def __repr__(self):

"""To have a better representation of the recipe"""

return f'{self.name}: {self.ingredients}'

def __hash__(self):

"""Allows us to use a Recipe object inside a set or as

a dictionary key. We assume that each recipe will have

a unique name, and therefore we can simply use the built-in

hash function on the name and return it as the hash id."""

return hash(self.name)

def __eq__(self,other):

"""For a recipe to equal another, their name and ingredients

must match"""

return self.name == other.name and dict(self.ingredients) == dict(other.ingredients)

PROBLEM:

image text in transcribed

def individually_feasible(self, recipes):

"""Returns a list of recipes that could be

individually made using the ingredients in the pantry.

Can be implemented in 9 lines of code."""

# YOUR CODE HERE

raise NotImplementedError()

Pantry.individually_feasible = individually_feasible

For the first three problems, we will be writing methods to extend the Pantry class, defined below, that holds cooking ingredients. Notice that Pantry is a subclass of AD Additionally, we will be using a Recipe class that specifies the ingredients that are required to make a dish. [] class Pantry(AD): def __init__(self, ingredients): """We initialize the Pantry class by passing the ingredients that were passed in to the initializer for the superclass, AD""" super() . _init_(ingredients) class Recipe: def __init__(self, name, ingredients): "We initialize the Recipe class, which stores the name of a recipe as well as the needed ingredients in the form of an AD""" self.name = name self.ingredients = AD(ingredients) def __repr__(self): "" "To have a better representation of the recipe"" return f'{self.name}: {self.ingredients)' def hash__(self): "" "Allows us to use a Recipe object inside a set or as a dictionary key. We assume that each recipe will have a unique name, and therefore we can simply use the built-in hash function on the name and return it as the hash id.""" return hash(self.name) def __e_(self, other): "" "For a recipe to equal another, their name and ingredients must match""" return self.name == other.name and dict(self.ingredients) == dict(other.ingredients) Problem 2: Checking recipe feasibility It would also be nice to know which recipes we can make given the current contents of our pantry. Your next task is to implement an individually_feasible method for the Pantry class, which takes a list of recipes and returns a list of the recipes that can be made individually using the ingredients in the pantry. For this problem, you do not need to worry about making multiple recipes at the same time. U def individually_feasible(self, recipes): "" "Returns a list of recipes that could be individually made using the ingredients in the pantry. Can be implemented in 9 lines of code.""" # YOUR CODE HERE raise Not ImplementedError() Pantry. individually_feasible = individually_feasible # Tests for Pantry.individually_feasible pantry5 = Pantry( {"chicken drumstick":5, "egg":10, "oil":6,"flour":8, "hot dog":3, "pepper":6}) fried chicken = Recipe ("Fried Chicken", {"chicken drumstick":1, "egg":2, "flour":1}) butter_fries = Recipe ("Butter Fries", {"potato":4, "butter":1, "oil":1}) boiled_egg - Recipe ("Boiled Egg", ("egg":1}) recipes = [fried_chicken, butter_fries, boiled_egg) # Order doesn't matter assert_equal(sorted (pantry5.individually_feasible (recipes), key=lambda x: x.name), [boiled egg, fried chicken) # There may be repeats in the input list assert_equal(pantry5.individually_feasible([boiled_egg, boiled_egg]), [boiled_egg, boiled_egg]) # If it's not possible to make any of the recipes in the input list, # individually_feasible should return an empty list pantry6 = Pantry({ "cucumber":1, "tomato":1}) spinach_salad = Recipe("Greek Salad", {"cucumber":1, "tomato":2, "feta cheese":1, "onion":0.3, "olive":10}) tomato sauce - Recipe ("Tomato Sauce", {"tomato":4, "garlic": 3, "salt":1, "oil":1}) assert_equal (pantry6.individually_feasible((spinach_salad, tomato sauce]), () For the first three problems, we will be writing methods to extend the Pantry class, defined below, that holds cooking ingredients. Notice that Pantry is a subclass of AD Additionally, we will be using a Recipe class that specifies the ingredients that are required to make a dish. [] class Pantry(AD): def __init__(self, ingredients): """We initialize the Pantry class by passing the ingredients that were passed in to the initializer for the superclass, AD""" super() . _init_(ingredients) class Recipe: def __init__(self, name, ingredients): "We initialize the Recipe class, which stores the name of a recipe as well as the needed ingredients in the form of an AD""" self.name = name self.ingredients = AD(ingredients) def __repr__(self): "" "To have a better representation of the recipe"" return f'{self.name}: {self.ingredients)' def hash__(self): "" "Allows us to use a Recipe object inside a set or as a dictionary key. We assume that each recipe will have a unique name, and therefore we can simply use the built-in hash function on the name and return it as the hash id.""" return hash(self.name) def __e_(self, other): "" "For a recipe to equal another, their name and ingredients must match""" return self.name == other.name and dict(self.ingredients) == dict(other.ingredients) Problem 2: Checking recipe feasibility It would also be nice to know which recipes we can make given the current contents of our pantry. Your next task is to implement an individually_feasible method for the Pantry class, which takes a list of recipes and returns a list of the recipes that can be made individually using the ingredients in the pantry. For this problem, you do not need to worry about making multiple recipes at the same time. U def individually_feasible(self, recipes): "" "Returns a list of recipes that could be individually made using the ingredients in the pantry. Can be implemented in 9 lines of code.""" # YOUR CODE HERE raise Not ImplementedError() Pantry. individually_feasible = individually_feasible # Tests for Pantry.individually_feasible pantry5 = Pantry( {"chicken drumstick":5, "egg":10, "oil":6,"flour":8, "hot dog":3, "pepper":6}) fried chicken = Recipe ("Fried Chicken", {"chicken drumstick":1, "egg":2, "flour":1}) butter_fries = Recipe ("Butter Fries", {"potato":4, "butter":1, "oil":1}) boiled_egg - Recipe ("Boiled Egg", ("egg":1}) recipes = [fried_chicken, butter_fries, boiled_egg) # Order doesn't matter assert_equal(sorted (pantry5.individually_feasible (recipes), key=lambda x: x.name), [boiled egg, fried chicken) # There may be repeats in the input list assert_equal(pantry5.individually_feasible([boiled_egg, boiled_egg]), [boiled_egg, boiled_egg]) # If it's not possible to make any of the recipes in the input list, # individually_feasible should return an empty list pantry6 = Pantry({ "cucumber":1, "tomato":1}) spinach_salad = Recipe("Greek Salad", {"cucumber":1, "tomato":2, "feta cheese":1, "onion":0.3, "olive":10}) tomato sauce - Recipe ("Tomato Sauce", {"tomato":4, "garlic": 3, "salt":1, "oil":1}) assert_equal (pantry6.individually_feasible((spinach_salad, tomato sauce]), ()

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

Repairing And Querying Databases Under Aggregate Constraints

Authors: Sergio Flesca ,Filippo Furfaro ,Francesco Parisi

2011th Edition

146141640X, 978-1461416401

More Books

Students also viewed these Databases questions

Question

2 What supply is and what affects it.

Answered: 1 week ago

Question

3 How supply and demand together determine market equilibrium.

Answered: 1 week ago