Question: Can you make a code implementing the following guidelines? Overview Food enthusiasts from around the world visit India to eat their native delicacies. But what

Can you make a code implementing the following guidelines?

Overview

Food enthusiasts from around the world visit India to eat their native delicacies. But what if you wanted to make them in your own home? What ingredients do you need? Which foods are vegetarian? These are common concerns in the topic of food. To understand this, it is important to realize the diversity of food in India. Different regions and states of India have their own versions of various foods including main courses, desserts, appetizers, etc. It is now time to explore these foods, their ingredients, and their region of origin!

Specifications

This project focuses on analyzing a publicly available dataset (slightly modified to remove edge cases) containing several hundred Indian foods, their region and state of origin, their necessary ingredients, etc. You will be using this dataset to build your own nested dictionaries to search the required information about regions, ingredients, vegetarian status, etc., which are outlined in various functions below.

Provide the following functions:

open_file() TextIO

Open a file for reading. Repeatedly prompt for a file name until a file is successfully opened (by catching the FileNotFoundError if it is raised). You likely have a similar copy from a previous project.

The prompt: 'Input a file name: '

The error message: 'Invalid filename, please try again.'

Returns: file_pointer: os.PathLike

Display: prompt and error message as appropriate

build_dictionary(file: os.PathLike) Dict

This function accepts the previously generated file pointer as input and returns the required dictionary. You may find either the csv.reader or the csv.DictReader class useful to read the data file. This function should iterate over the CSV reader and, with each iteration, extract the necessary data and to create a dictionary that holds the data. Remember to skip the header line(s) if using csv.reader. If there is no data available, as indicated by the string '-1' in any of the columns, ignore that line of input. The data to be extracted is as follows (note that the "column" information starts counting at 1 whereas the indices start at 0):

Column Contents
1 Food name
2 Ingredients
3 Diet
4 Prep time
5 Cooking time
6 Flavor
7 State
8 Region

The ingredients are a series of strings separated by a comma. Convert only all ingredients to lowercase so that "Milk" is the same as "milk," for example. Also, remove all leading and trailing spaces from a name. The ingredients should be stored in a set. The structure of the dictionary is as follows:

{'East': {'West Bengal': {'Balu shahi': [{'maida flour','oil','sugar','yogurt'},'vegetarian', (45, 25),'sweet'], 'Gulab jamun': [{'baking powder','ghee','milk','milk powder','plain flour','rose water','sugar','water'},'vegetarian',(15, 40),'sweet'], ...

The structure can be summarized as follows:

dict dict dict List

Or, if you prefer:

dict[str, dict[str, dict[str, list[set[str], str, tuple[int, int], str]]]]

In detail, the structure takes the following columns (where the arrow signifies nesting):

Region State Food name [set(ingredients), diet, (prep time, cooking time), flavor]

{'East': {'West Bengal': {'Balu shahi': [{'maida flour','oil','sugar','yogurt'},'vegetarian', (45, 25),'sweet'], 'Gulab jamun': [{'baking powder','ghee','milk' 'milk powder','plain flour','rose water','sugar','water'},'vegetarian',(15, 40),'sweet'], ...

Preparation and cooking time should be stored as integers in a tuple.

Note: Note: a shorter version of the data file name 'indian_food_small.csv' has been provided to simplify testing and debugging.

get_ingredients(dict, list) set

This function takes in a list (of food names) and a dictionary (from build_dictionary), and returns a set of ingredients that are required to make all of the indicated foods. Find the foods in the dictionary and return the set of ingredients required to make them. Hint: Use the concepts of sets and their operations such as intersection and union to make this function. Return these ingredients as a set.

get_useful_and_missing_ingredients(dict, list, list) tuple

This function takes 3 parameters: the dictionary from build_dictionary, a list of foods you want to make, and a list of ingredients that you currently have (in that order). The function returns a tuple of two lists: useful ingredients and missing ingredients. Given a list of foods and ingredients, the goal of this function is to find out which ingredients are useful and which are missing.

Useful ingredients are defined as follows: ingredients which you have (list of ingredients), and are also needed to make the list of foods (the set of ingredients for all the foods).

Missing ingredients are defined as follows: ingredients which you need to make the list of foods, but you currently do NOT have.

Return these as a tuple of two lists (the lists must be sorted in alphabetical order). Hint: use the previous function get_ingredients and call it within this function to make your life easier (set operations are very useful for this task! Also, when you call sorted() on a set, it returns a sorted list.

Here's an example:

foods = [Kalakand', 'Lassi'] ingredients_you_have = ['milk', 'sugar', 'ghee', 'rice', 'nuts'] get_useful_and_missing_ingredients(superD, foods, ingredients_you_have) Return (ingredients_you_have_and_need, ingredients_to_purchase) (['milk', 'nuts', 'sugar'], ['cottage cheese', 'yogurt'])

Refer to the .csv file to look at the ingredients and try to make sense of what is being asked of you for this function.

get_list_of_foods(dict, list) list

This function takes in a list of ingredients as a parameter and the dictionary from build_dictionary. It then returns a list of all possible foods that can be made given the ingredients. Iterate through every food in the dictionary and check to see if you have ALL the ingredients to make that food. If so, add it to your list of foods that you will return. Hint: Use set operations such as subsets and supersets to make your life easier. Return the food names as a list. Sort the foods in order of increasing total cooking time (remember total cooking time is prep time + cook time). Resolve any cooking-time ties by sorting the foods in alphabetical order (which should happen by default). Hints: (1) sort by cooking time then by name, (2) you can sum all element in a tuple by using the sum function, e.g. sum(tup))

get_food_by_preference(dict, list) list

This function takes in the dictionary from build_dictionary and a list of preferences as parameters. It then returns a list of foods which match the conditions listed in the list of preferences sorted alphabetically. These conditions include region, state, diet (vegetarian or not vegetarian), and flavor. Hint: Use set addition and set differences to make your life easier. Sort the foods in alphabetical order and return them as a list.

Preferences are listed in the following order in the preferences parameter:

[Region, State, Diet (vegetarian status), Flavor]

Note, use None if there is no preference for a certain variable. For example, an example preference list could be as follows:

[None, None, 'non vegetarian', 'sweet']

The above list says that the user does not care what region or state (because both are None), but wants to filter out foods that are 'non vegetarian' and 'sweet' only.

main()

This function calls open_file() with the string parameter 'indian_food' and uses the file pointer to build the dictionary. Then use the dictionary to do various tasks using all of the functions you have written so far. Print the provided MENU and ask the user to enter one of 5 choices (A, B, C, D, Q). The program should accept lower and upper case inputs. If a user enters an invalid input, print an error message "Invalid input. Please enter a valid input (A-D, Q)". After the program successfully exits (if the user selects choice Q from the menu), print closing message "Thanks for playing!" and exit the program. Otherwise, keep asking the user for another choice!

If the choice is A: the program asks to input various foods separated by a comma ( 'Enter foods, separated by commas: '). Then, print the phrase 'Ingredients: '. Then, the program prints out all the ingredients required to make these foods in alphabetical order separated by comma and a leading space (hint: use get_ingredients() to get all the ingredients. Note that the food names should not have any leading or tailing spaces when calling the get_ingredients function).

If the choice is B: the program asks to input various ingredients separated by a comma ('Enter ingredients, separated by a comma: '). First print the phrase 'Foods: '. Then, the program prints out all the foods you can make separated by comma and a leading space by utilizing the function get_list_of_foods(). Note that the ingredient names should not have any leading or tailing spaces when calling the get_list_of_foods function).

If the choice is C: the program first asks the user to input various foods separated by a comma ( 'Enter foods, separated by commas: '). Second, the program asks to input various ingredients separated by aget_food_by_preference comma ('Enter ingredients, separated by commas: '). Get the useful and missing ingredients by calling get_useful_and_missing_ingredients(). Finally, the program prints out all the useful and missing ingredients. First, print the phrase 'Useful Ingredients: '. Then, print all the useful ingredients separated by commas and a leading space. After that, print the phrase 'Missing Ingredients: '. Finally, print out all the missing ingredients separated by commas and a leading space. Note that the ingredient and food names should not have any leading or tailing spaces when calling the get_useful_and_missing_ingredients function).

If the choice is D: the program asks to input various preferences separated by a comma ( 'Enter preferences, separated by commas: '). First, print the phrase 'Preferred Food: '. Finally, the program prints out all the foods that match the preferences from the dictionary by utilizing the function get_food_by_preference separated by commas and a leading space. Note that the preferences should not have any leading or tailing spaces when calling the get_food_by_preference function).

Assignment Notes

A starter file, project_1.py, that has the functions stubbs for the required functions hasbeen provided.

Using the CSV package: Remember import csv

reader = csv.reader(fp) # attaches a reader to the file fp next(reader,None) # skips a line, such as a header line for line in reader: # line is a list

Use itemgetter() from the operator module to specify the key for sorting. For example, sorted(L,key=itemgetter(1,2)) return a sorted list on index 1 of the lists or tuple then sorted by index 2 to break ties.

The set class has 2 methods: .issuperset() and .issubset(). issuperset() returns True if a set has every elements of another set (passed as an argument). If not, it returns False. issubset() returns True if all items in the set exists in the specified set, otherwise it returns False.

Submit your solution in a file named lastname_firstname_lab_1.py

Example Run

Indian Foods & Ingredients. Input a indian_food file: indian_food.csv A. Input various foods and get the ingredients needed to make them! B. Input various ingredients and get all the foods you can make with them! C. Input various foods and ingredients and get the useful and missing ingredients! D. Input various foods and preferences and get only the foods specified by your preference! Q. Quit : A Enter foods, separated by commas: Rasgulla, Ariselu ,Kheer Ingredients: cardamom, chhena, ghee, jaggery, rice flour, sugar A. Input various foods and get the ingredients needed to make them! B. Input various ingredients and get all the foods you can make with them! C. Input various foods and ingredients and get the useful and missing ingredients! D. Input various foods and preferences and get only the foods specified by your preference! Q. Quit : k Invalid input. Please enter a valid input (A-D, Q) A. Input various foods and get the ingredients needed to make them! B. Input various ingredients and get all the foods you can make with them! C. Input various foods and ingredients and get the useful and missing ingredients! D. Input various foods and preferences and get only the foods specified by your preference! Q. Quit : b Enter ingredients, separated by commas: chhena,sugar,ghee , flour,nuts, jaggery Foods: Chhena jalebi, Ledikeni, Pantua A. Input various foods and get the ingredients needed to make them! B. Input various ingredients and get all the foods you can make with them! C. Input various foods and ingredients and get the useful and missing ingredients! D. Input various foods and preferences and get only the foods specified by your preference! Q. Quit : Q 

here is the csv file provided:

Can you make a code implementing the following guidelines? Overview Food enthusiasts

name, ingredients, diet, prep_time, cook_time, flavor_profile, course, state, region Balu shahi, "Maida flour, yogurt, oil, sugar", vegetarian,45,25, sweet, dessert, West Bengal, East Boondi, "Gram flour, ghee, sugar", vegetarian, 80, 30, sweet, dessert, Rajasthan, West Gajar ka halwa, "Carrots, milk, sugar, ghee, cashews, raisins", vegetarian, 15, 60, sweet, dessert, Punjab, North Ghevar, "Flour, ghee, kewra, milk, clarified butter, sugar, almonds, pistachio, saffron, green cardamom", vegetar Chikki, "Peanuts, jaggery", vegetarian, 10, 20, sweet, dessert, Maharashtra, West Dharwad pedha, "Milk, Sugar, Dharwadi buffalo milk", vegetarian, 20, 60, sweet, dessert, Karnataka, South Kajjikaya, "Rice flour, jaggery, coconut", vegetarian, 40, 15, sweet, dessert, Andhra Pradesh, South Anarsa, "Rice flour, jaggery, khus-khus seeds", vegetarian, 10, 50, sweet, dessert, Maharashtra, West Basundi, "Sugar, milk, nuts", vegetarian, 10, 35, sweet, dessert, Gujarat, West Lassi, "Yogurt, milk, nuts, sugar", vegetarian, 5, 5, sweet, dessert, Punjab, North Kheer, "Milk, rice, sugar, dried fruits", vegetarian, 10,40 , sweet, dessert, 1,1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!