Question

In: Computer Science

Python A good friend of yours sent you a long list of recipe names in a...

Python

A good friend of yours sent you a long list of recipe names in a text file (where each line is a recipe that also includes information about how long it takes to do the recipe). You only want to know about the recipes that involve chocolate and are fast (i.e., take 15 mins or less to prepare). Write the program for this!

Write a program to -
      + Find recipes that involve chocolate that can be done in 15 mins or less (aka "fast chocolate" recipes) from the file named my-recipes.txt (attachment)
      + Print out the total number of "fast chocolate" recipes
      + Print out that list of "fast chocolate" recipes in a readable form (i.e., line by line, without extra whitespace like newlines).

Your output should look something like -

      We found 5 fast chocolate recipes!
      Here they are:
      Chocolate Covered Oreos | 5 mins
      Chocolate Pudding | 8 mins
      Chocolate Cheesecake Bars | 15 mins
      Chocolate-Covered Cake Balls | 12 mins
      Death by Chocolate Brownies | 10 mins

my-recipes.txt:

Samoa Dessert Lasagna | 45 mins
Birthday Cake | 25 mins
Oreo Truffles | 15 mins
Chocolate Covered Oreos | 5 mins
Chocolate Pudding | 8 mins
Vanilla Pudding | 10 mins
Cookie Dough Ice Cream Sandwiches | 15 mins
Neapolitan Bundt Cake | 15 mins
Swiss Roll Cake | 15 mins
Chocolate Cheesecake Bars | 15 mins
Confetti Squares | 20 mins
Chocolate-Covered Cake Balls | 12 mins
Chess Pie | 15 mins
Brownie Lasagna | 25 mins
Chocolate-Covered Cherries | 18 mins
Pumpkin Cheesecake Roll | 15 mins
Reese's Stuff Rice Krispies Treats | 15 mins
Death by Chocolate Poke Cake | 35 mins
Blueberry Cheesecake Egg Rolls | 15 mins
Peanut Butter No-Bake Cookies | 15 mins
Strawberry Cheesecake Stuffed Cupcakes | 15 mins
No-Bake Cheesecake | 15 mins
Mrs. Fields Chocolate Chip Cookies | 20 mins
Nutella Pops | 15 mins
Death by Chocolate Brownies | 10 mins
S'mores Grilled Cheese | 15 mins
Cookie Dough Fudge | 15 mins
Checkerboard Cake | 25 mins
Cookiedilla | 10 mins
S'mores Poke Brownies | 15 mins

Solutions

Expert Solution

import re

chocolate_recipes = list()
fast_chocolate_recipes = list()
filename = "my-recipes.txt"
regexp = re.compile(r'[cC]hocolate')

# Open the txt file and put each of the recipes in a list
with open(filename) as f:
    recipes = f.readlines()
recipes = [recipe.strip() for recipe in recipes]

# Search for the word "Chocolate"/"chocolate" using regex in the recipes list 
# and put all chocolate recipes in one single list
for recipe in recipes:
    if regexp.search(recipe):
        chocolate_recipes.append(recipe)

# For each chocolate recipes, strip out the portion before "|" and also strip out the mins (the number) taken
# from the remaining string. Covert the number from str to int and check if its a fast chocolate recipe
for chocolate_recipe in chocolate_recipes:
    time = chocolate_recipe.split('| ')[1].split(' mins')[0]
    if int(time) <= 15:
        fast_chocolate_recipes.append(chocolate_recipe)

print(f"We found {len(fast_chocolate_recipes)} fast chocolate recipes!")
print("Here they are: ")
for fast_chocolate_recipe in fast_chocolate_recipes:
    print(fast_chocolate_recipe)

Related Solutions

Let’s imagine a good friend of yours started a restaurant and wants you to invest a...
Let’s imagine a good friend of yours started a restaurant and wants you to invest a significant amount of money in his/her business. 1. Which financial statement(s) that were introduced in Chapter 1 Accounting and the Business Environment would be relevant to your decision making and why? 2.You are also given a chance to ask his/her accountant three questions to help you make the decision. What would you ask and why?
a) A good friend of yours is asking you whether she/he should undergo psychoanalysis or client-centered...
a) A good friend of yours is asking you whether she/he should undergo psychoanalysis or client-centered therapy. Which factors would you consider before answering her/him, and what would be your final recommendation based on these factors? (write 400 words) b) Do you think humanistic psychology was right in taking the stance that psychology needed a 'third force' to counteract the 'dark side' of psychology (psychoanalysis)? write in-depth c) Why are Bühler, Kohlberg, Erikson, and Gilligan's approaches to development regarded as...
1. A good friend of yours is asking you whether she/he should undergo psychoanalysis or client-centered...
1. A good friend of yours is asking you whether she/he should undergo psychoanalysis or client-centered therapy. Which factors would you consider before answering her/him, and what would be your final recommendation based on these factors? Please write in-depth as much as you can and answer each part of the question. Please answer this question by your thinking rather than copying the whole from the material you already post. Thus, make the answer by your own knowledge if you have...
1. A good friend of yours is asking you whether she/he should undergo psychoanalysis or client-centered...
1. A good friend of yours is asking you whether she/he should undergo psychoanalysis or client-centered therapy. Which factors would you consider before answering her/him, and what would be your final recommendation based on these factors? (write 400 words and as you are writing and what would suggest so answer properly) Please answer this question by your thinking rather than copying the whole from the material you already post. Thus, make the answer by your own knowledge if you have...
Consider the case of​ Rena's kidney. A good friend of​ yours, Rena​ Elle, needs a kidney....
Consider the case of​ Rena's kidney. A good friend of​ yours, Rena​ Elle, needs a kidney. There are two potential​ volunteers: Cameron and Simone. If one donates and the other does​ not, the one who free rides gets​ 5,000 of​ benefit; the one who donates gets​ 5,000 of benefit but suffers​ 4,000 of​ costs, for a net gain of​ 1,000. If Cameron and Simone both ​donate, surgeons take both their kidneys and choose the healthiest one for Rena.​ Then, both...
A friend of yours says that she has found a lump in her breast. List steps...
A friend of yours says that she has found a lump in her breast. List steps that she would take from initial types of diagnostic testing to surgery to treatment for stage 3 breast cancer. What types of complementary medicine or dietary and lifestyle changes can she make to improve her chances of beating this cancer.
1. A friend of yours claims to be psychic. You are skeptical. To test this you...
1. A friend of yours claims to be psychic. You are skeptical. To test this you take a stack of 100 playing cards and have your friend try to identify the suit (hearts, diamonds, clubs, or spades), without looking, of course! State the null hypothesis for your experiment. b. State the alternative hypothesis. 2. Suppose your psychic friend correctly identified more than 25% of the cards. A hypothesis test gave a P-value of 0.014. What do you conclude? b. What...
Using LIST and FUNCTION Write a program in Python that asks for the names of three...
Using LIST and FUNCTION Write a program in Python that asks for the names of three runners and the time it took each of them to finish a race. The program should display who came in first, second, and third place.
Write a program that uses Python List of strings to hold the five student names, a...
Write a program that uses Python List of strings to hold the five student names, a Python List of five characters to hold the five students’ letter grades, and a Python List of four floats to hold each student’s set of test scores. The program should allow the user to enter each student’s name and his or her four test scores. It should then calculate and display each student’s average test score and a letter grade based on the average....
Write a program using C to read a list of your friend names which ends by...
Write a program using C to read a list of your friend names which ends by the word end. The program builds a linked list using these names and prints the names in the order stored into the linked list The list can be created using insertion at the beginning or insertion at the end; Use switch case to select the type of insertion; Case 1:insertion in the beginning; Case2:insertion in the end. Once the list is printed after insertion;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT