Question

In: Computer Science

Write code that would go where the comment "#your line will go here" so that the...

Write code that would go where the comment "#your line will go here" so that the code would successfully extract all names of dishes from the dictionary popular_foods that have at least one of the following ingredients:

popular_foods = {"Clam Chowder":
{"Country": "United States",
"Main Ingredients": ["clam", "onion",
"celery", "potato", "carrot",
"butter", "flour", "cream",
"red wine vinegar"]},
"Xiaolongbao":
{"Country": "China",
"Main Ingredients": ["flour", "pork",
"crab meat", "roe", "soy sauce",
"ginger", "rice wine",
"green onions"]},
"Feijoada":
{"Country": "Brazil",
"Main Ingredients": ["black beans",
"pork", "cabbage", "red peppers",
"onion", "tomato", "garlic"]},
"Ash Reshteh":
{"Country": "Iran",
"Main Ingredients": ["kidney beans",
"chickpeas", "navy beans",
"parsley", "spinach", "coriander",
"dill", "persian reshteh noodles",
"onions", "flour", "mint", "kashk",
"turmeric"]},
"Draniki":
{"Country": "Russia",
"Main Ingredients": ["potato", "onion",
"egg", "flour", "garlic"]},
"Ata Dindin":
{"Country": "Nigeria",
"Main Ingredients": ["palm oil",
"red bell pepper",
"scotch bonnet", "tomato",
"onion", "chicken stock",
"crayfish", "prawns", "mackerel"]}
}

dishes = []

for dish in popular_foods:
food_search = ["potato", "pork", "garlic"]
for food in food_search:
# your line will go here
dishes.append(dish)
break

 

Solutions

Expert Solution

The statement that fills the given code is

if food in popular_foods[dish]['Main Ingredients']:

Justification for the above statement:

food is an element in food_search list popular_foods[dish]['Main Ingredients'] means, is checks for the dish name that has the Main Ingredient from the food list.

The given popular_foods dictionary is nested in the following way:

popular_food = {"dish name":{"Country":"country name", "Main Ingredients":["ingredient1", "ingredient2", ...]}}

to check if the ingredient is present in a particular dish or not we use the followin code:

dishes = [] # an empty list that is later used to store the dish names that contain the given ingredients

# the following loop is used to iterate over every dish name in the popular_foods dictionary

for dish in popular_foods:

#the following is a list of items that are to searched in the given dictionary
   food_search = ["potato", "pork", "garlic"]

#the following loop iterates over the above list of items
   for food in food_search:

#checks if the item in food_search is present in the Main Ingredients of each dish in the popular_foods or not
       if food in popular_foods[dish]['Main Ingredients']:

# if any of the item from food_searc is in the Main Ingredients of popular_food, then that dish name is appended to the list dish and the loop breaks
           dishes.append(dish)
           break

Code:

popular_foods = {"Clam Chowder":
{"Country": "United States",
"Main Ingredients": ["clam", "onion",
"celery", "potato", "carrot",
"butter", "flour", "cream",
"red wine vinegar"]},
"Xiaolongbao":
{"Country": "China",
"Main Ingredients": ["flour", "pork",
"crab meat", "roe", "soy sauce",
"ginger", "rice wine",
"green onions"]},
"Feijoada":
{"Country": "Brazil",
"Main Ingredients": ["black beans",
"pork", "cabbage", "red peppers",
"onion", "tomato", "garlic"]},
"Ash Reshteh":
{"Country": "Iran",
"Main Ingredients": ["kidney beans",
"chickpeas", "navy beans",
"parsley", "spinach", "coriander",
"dill", "persian reshteh noodles",
"onions", "flour", "mint", "kashk",
"turmeric"]},
"Draniki":
{"Country": "Russia",
"Main Ingredients": ["potato", "onion",
"egg", "flour", "garlic"]},
"Ata Dindin":
{"Country": "Nigeria",
"Main Ingredients": ["palm oil",
"red bell pepper",
"scotch bonnet", "tomato",
"onion", "chicken stock",
"crayfish", "prawns", "mackerel"]}
}

dishes = []

for dish in popular_foods:
   food_search = ["potato", "pork", "garlic"]
   for food in food_search:
       if food in popular_foods[dish]['Main Ingredients']:
           dishes.append(dish)
           break

Code in the image:


Related Solutions

public class Sum2 { // TODO - write your code below this comment. } Download the...
public class Sum2 { // TODO - write your code below this comment. } Download the Sum2.java file, and open it in jGrasp (or a text editor of your choice). This program takes two values as command-line arguments, converts them to ints via Integer.parseInt, and adds them together. Example output of this program is shown below, with the command-line arguments 3 4: Sum: 7
public class FirstChar { // TODO - write your code below this comment. } Download the...
public class FirstChar { // TODO - write your code below this comment. } Download the FirstChar.java file, and open it in jGrasp (or a text editor of your choice). This program takes a single command-line argument and prints out the first character of this argument, using String's charAt() method. If you're unsure how to pass command-line arguments to a program with jGrasp, see this tutorial. Example output of this program with the command-line argument foo is shown below: First...
Here is what I have so far. I have created a code where a user can...
Here is what I have so far. I have created a code where a user can enter in their information and when they click submit all of the information is shown. How can I add a required field for the phone number without using an alert? <!Doctype html> <html> <head> <meta charset="UTF-8"> <title>Login and Registeration Form Design</title> <link rel="stylesheet" type="text/css" href="signin.css"> <script> function myFunction(){ document.getElementById('demo').innerHTML = document.getElementById('fname').value + " " + document.getElementById('lname').value + " " + document.getElementById('street').value + " "...
Using the provided code (found here), write a program using the main method where the user...
Using the provided code (found here), write a program using the main method where the user enters Strings and the program echoes these strings to the console until the user enters “quit”. When user quits the program should print out, “Goodbye”. You may assume that the case is ignored when the user enters, “quit”, so “quit”, “QUIT”, “Quit”,“qUiT”, etc. are all acceptable ways to end the program. The results should be printed out to the console in the following format:...
Please write here not in your hand like picture. Please go to the following link and...
Please write here not in your hand like picture. Please go to the following link and post their comments. https://www.khanacademy.org/economics-finance-domain/macroeconomics/income-and-expenditure-topic/is-lm-model-tutorial/v/investment-and-real-interest-rates
C# programming. Comment/Explain the below code line by line. I am having a hard time following...
C# programming. Comment/Explain the below code line by line. I am having a hard time following it. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Nth_prime {     class Program     {         public static bool isPrime(int number)         {             int counter = 0;             for (int j = 2; j < number; j++)             {                 if (number % j == 0)                 {                     counter = 1;                     break;                 }             }             if (counter == 0)             {                 return true;             }             else             {                 return false;             }         }...
Write this code in java and don't forget to comment every step. Write a method which...
Write this code in java and don't forget to comment every step. Write a method which asks a baker how hot their water is, and prints out whether it is OK to make bread with the water. If the water is at or above 110F, your method should print "Too Warm." If the water is below 90.5F, print "Too Cold." If it is in between, print "Just right to bake!" For example, if the user inputs the number 100.5, the...
QUESTION 6 A single line comment in C++ language source code can begin with _____   ...
QUESTION 6 A single line comment in C++ language source code can begin with _____        a) // b) ; c) : d) /* QUESTION 7 What is the output of the following program? #include<iostream> using namespace std; class abc { public: int i; abc(int i) { i = i; } }; main() { abc m(5); cout<<m.i; } a)Garbage b)5        c)Compile Error d)None of the answers QUESTION 8 The following operator can be used to calculate the...
Please write code in java and comment . thanksItem classA constructor, with a String...
Please write code in java and comment . thanksItem classA constructor, with a String parameter representing the name of the item.A name() method and a toString() method, both of which are identical and which return the name of the item.BadAmountException ClassIt must be a RuntimeException. A RuntimeException is a subclass of Exception which has the special property that we wouldn't need to declare it if we need to use it.It must have a default constructor.It must have a constructor which...
So previously in my class I had to write a python program where the user would...
So previously in my class I had to write a python program where the user would enter the price for 5 different items, and it would calculate the subtotal, total tax owed and the total amount owed. Now I need to write a program that would do the same but would be done using modules in the code, I am having a hard time figuring modules out. Below is the code from the original program not using modules: # Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT