Question

Please, write code that would go where the comment "#your line will go here" so that...

Please, 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:
if ingr in food_search:
dishes.append(dish)
break

Did I right this our right?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Ok, so we are given a dictionary of Popular_Foods which shows us two things, the first part shows the country of the Dish and the second part tells us about the ingredients used in that particular dish.

So, basically the given dictionary named popular_foods is a nested dictionary, i.e, for each dish name we have two dictionaries first one for the country name and second one for the list of main ingredients used in dish.

dish name country main ingredients name of country list of ingredients

Now, we need to get the name of the dishes in popular foods whose main ingredients contains any of the following

Potato or Garlic or Pork

For this we need to iterate through the Main ingredients of each of the dishes and select it if it contains either of the ingredients we are looking for (potato, garlic, pork)

Firstly, we will initiate our list of ingredients we are looking for, and also we'll initiate the dishes list in which we'll append the dishes containg ingredients of our choice

givenIngr = ["potato", "pork", "garlic"] #list of ingredients we are searching for
dishes = []#we'll append our dishes here

After this we'll go to each of the dishes and in it's list of main ingredients we'll check if it contains any of our "givenIngr", if contains even one of our list of ingredients then we'll append the dish to our list of dishes and break out to next dish

for key in popular_foods: #key  is the name of dish
    dictOfDish = popular_foods[key] #each dish is a list of dictionary
    listOfMainIngr = dictOfDish['Main Ingredients'] #we're interested only in main ingredient
    for x in listOfMainIngr:
        if (x in givenIngr and key not in dishes):
            dishes.append(key)
            break #if any of the Main Ingr matches with our list of givenIngr

Finally we can print the list of dishes which contains either of [potato,garlic,pork]

print(dishes)

Here is our final code:

popular_foods = {"Clam Chowder": #dictionary of popular foods
{"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"]}
}
         


givenIngr = ["potato", "pork", "garlic"] #list of ingredients we are searching for
dishes = []#we'll append our dishes here

for key in popular_foods: #key  is the name of dish
    dictOfDish = popular_foods[key] #each dish is a list of dictionary
    listOfMainIngr = dictOfDish['Main Ingredients'] #we're interested only in main ingredient
    for x in listOfMainIngr:
        if (x in givenIngr and key not in dishes):
            dishes.append(key)
            break #if any of the Main Ingr matches with our list of givenIngr
        
print(dishes)
    
    
    

output:

Shell [Clam Chowder, Xiaolongbao, Feijoada, Draniki]

Add a comment
Know the answer?
Add Answer to:
Please, write code that would go where the comment "#your line will go here" so that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I couldn't find where to comment. But it has to be written in C++!!!!! Task-1: Rational...

    I couldn't find where to comment. But it has to be written in C++!!!!! Task-1: Rational fractions are of the form a / b, in which a and b are integers and ! ≠ 0. In this exercise, by ‘‘fractions’’ we mean rational fractions. Suppose a / b and c / d are fractions. Arithmetic operations and relational operations on fractions are defined by the following rules: Arithmetic Operations: a/b + c/d = (ad + bc)/bd a/b – c/d =...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT