Question
(d) (12 marks) Implement the free from ingredient function as a function free_from in Python code. Make sure to include a docstring. Run the code
(d) (12 marks)
Implement the free from ingredient function as a function free_from in Python code. Make sure to include a docstring. Run the code in the cell below to test your implementation. Add further code to test your implementation on your own three test cases. You should show the code and the output from all four test cases, whether the test works or fails.
#Write your implementation here
# Run this code to test your implementation test_menu = [ ['potato'], ['cheese'], ['apple'] ] expected_result = True if free_from(test_menu, 'water') == expected_result: #adjust the function call if necessary to match your implementation print('Working') else: print('Failure')
# Add code here to test your implementation on your further three test cases
The question before this was
''
(b) (8 marks)
Define a function that takes a menu comprising three dishes, and a specified ingredient, and outputs true if all the dishes on the menu are free from the specified ingredient. If any of the dishes on the menu contain the specified ingredient, the function outputs false.
A menu is represented as a list of three dishes.
A dish is represented as a non-empty list of non-empty strings, the first string being the name of the dish and the remaining strings being the ingredients of the dish.
A dish can have a name but no listed ingredients. Ingredients can be duplicated. If the name of the dish matches the specified ingredient, the function should output false.
For example, the menu:
Dish | Ingredients |
---|---|
soup | onion, potato, leek, celery |
pizza | bread, tomato, cheese, cheese |
banana |
would be represented in Python as:
menu = [ ['soup','onion','potato','leek','celery'], ['pizza','bread','tomato','cheese','cheese'], ['banana'] ]
This example menu is not free from banana.
Function: free from ingredient Inputs: Preconditions: Output: Postconditions:
(c) (10 marks)
Add three different test cases to the following test table for the free from ingredient function, where water is the specified ingredient.
We have started with a case where all three dishes have no ingredients. State, giving a reason, whether this is an edge case.
case | menu | ingredient | output |
---|---|---|---|
all three dishes have no ingredients | (('potato'), ('cheese'), ('apple')) | 'water' | True |
|
''
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started