Need help on problem 2 & 3 in the images shown below, specifically for the functions “individually_feasible” and “simul_feasible_nr”. I’m stuck on how to do those problems.
5 attachments
Slide 1 of 5
attachment_1
attachment_1
attachment_2
attachment_2
attachment_3
attachment_3
attachment_4
attachment_4
attachment_5
attachment_5
UNFORMATTED ATTACHMENT PREVIEW
Testing Make sure to run the following cell to make sure that the Python testing framework, nose, is installed. (It’s required for the rest of the notebook to work.) [22] try: from nose.tools import assert_equal, assert_almost_equal from nose. tools import assert_true, assert_false from nose. tools import assert_not_equal, assert_greater_equal except: !pip install nose from nose. tools import assert_equal, assert_almost_equal from nose.tools import assert_true, assert_false from nose. tools import assert_not_equal, assert_greater_equal [23] 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) r = @staticmethod def _binary op(left, right, op, neutral): 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 [49] class Pantry(AD): definit__(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: definit__(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 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 a individually_feasible method for the Pantry class, which takes a list of recipes and returns a list of the recipes that can be mad individually using the ingredients in the pantry. For this problem, you do not need to worry about making multiple recipes at the same time. 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 Pantry.individually_feasible = individually_feasible Problem 3: Checking simultaneous recipe feasibility Similar to the previous problem, we want to know recipes we can make given the current contents of our pantry, but this time, for multiple recipes at once. For this problem, you will implement a simul_feasible_nr method for the Pantry class. This method takes a list of recipes and returns a list containing all of the different combinations of recipes that could be simultaneously made using the available ingredients in the pantry, without repeating any recipe in each combination. (The “nr” in the name stands for “non-repeating”.) simul_feasible_nr should return a list of lists. That is, the returned value should be a list, where each element in that list is itself a list representing a particular combination of recipes. Neither the order of the outer list nor the order of the inner list matters. A combination of recipes has a minimum of 1 recipe and has no maximum size, as long as the pantry has enough ingredients. Hints: • simul_feasible_nr may call the individually_feasible method defined above. • You may find it useful to define an inner function that calls itself recursively. [] def simul_feasible_nr(self, recipes): “””Returns a list containing different combinations of recipes that could be made simultaneously using the ingredients available in the pantry.” # YOUR CODE HERE raise NotImplementedError() Pantry.simul_feasible_nr = simul_feasible_nr If simul_feasible_nr is working correctly, then the below cell should print something like this:
User generated content is uploaded by users for the purposes of learning and should be used following Studypool’s honor code & terms of service.
This question has not been answered.
Create a free account to get help with this and any other question!
VIEW ANSWER
24/7 Homework Help
Stuck on a homework question? Our verified tutors can answer all questions, from basic math to advanced rocket science!
Similar Content
Python Movie Genres Survey Program
CS 210 University of New Hampshire Programming Calculator Python Code Project
Richland Community College Java Programming Code
Massachusetts Institute of Technology Road Trip JAVA Programming Design Document
l o a d ( ” grap h s . s a g e ” ) n e i g h b o r s ( ghost , 6 )…
In Java, an array list is a part of a collection that is contained in the java.util framework. It…
Complete The To Do Activities Create An Entity Relationship E R Diagram For The Scenario