In: Computer Science
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
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)