Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a greedy algorithm using Python Imagine that you walk into a restaurant, a

ID: 3605950 • Letter: W

Question

Write a greedy algorithm using Python

Imagine that you walk into a restaurant, and want to maximize your caloric intake while staying under your budget of 20 dollars. You step to the counter and see your options of (with each item listed as Name, Price, Calories) . Water, $1.00, 0 Soda, $2.00, 100 Salad, $3.50, 200 . Fries, $4.00, 400 Burger, $6.50, 700 Chicken, $7.00, 500 Ice Cream, $3.00, 400 Please write and implement a greedy algorithm that maximizes your calories while staying under budget. You can do this in the notebook or in a separate file You should test three methods of choosing the "best" item: .The lowest price The highest calories The best calorie/price ratio Your code should include: -A definition of a class (Food) that stores the data for each item, and returns the proper value needed for each "best" method A function that takes in a list of names, prices and calories and returns a menu (a list of Foods) An algorithm that sorts your list, using the three condition described above, from best to worst. It should return a new sorted list, not changing the original · Code that keeps a running sum of the calories and cost of your meal. This code will stop building a meal if the cost is over budget. Your code should be flexible enough to change the budget (input it as a parameter)

Explanation / Answer

No, its not possible

Let two process be P1, P2

Let three resources be R1, R2, R3

Case 1: P1 and P2 needs <=1 resources each.

no deadlock because we have 3 resources whereas we need max. 2 resources total

Case 2: P1 needs 1 and P2 needs 2 resources (or vice versa)

no deadlock since we have 3 resources and we need 3 resources.

Case 3: P1 and P2 both need 2 resources.

no deadlock again because we have 3 resources and we need max. 4 hence at a time only one process will be waiting for the resource to get free and when only one process is waiting then deadlock does not arise. (for a deadlock to happen at least 2 process should be waiting at the same time).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote