Activity 2: Burger
Topic Focus
This activity extends an overarching activity introduced in the labs. It provides an emphasis on String Operations to solve subproblems, as well as forming correct expressions to use in conditional statements or loop structures for checking necessary criteria.
Nowadays, some foods can be customized to be unique from customer to customer if needed. For example, one may opt to order a burger with extra or no cheese at all.
The following table details the price of each ingredient that can be added to a burger:
| Ingredient | Identifier | Price ($) |
|---|---|---|
| Bun | B | 0.50 |
| Cheese | C | 0.80 |
| Patty | P | 1.50 |
| Veggies | V | 0.70 |
| Onions | O | 0.40 |
| Mushrooms | M | 0.90 |
Task 1
A lofty definition of a burger is at least ONE (1) patty with some other known ingredients if any in between two sesame seed buns.
Write a function called is_burger(burger), which takes in a string of characters burger and determines if it represents a valid burger.
This function should return True if burger is valid and False otherwise.
Examples of valid burger strings include "BVPB", "BMPPMB", "BCPOMB", and "BVVVVVPMB".
Examples of invalid burger strings include "BB", "BCPV", "PVOMB", and "BLIMP", where "L" and "I" do not represent valid ingredients as based on the given table.
A sample output is as follows:
>>> is_burger("BVPB")
True
>>> is_burger("BCPV")
False
>>> is_burger("BLIMP")
False
>>> is_burger("BCPOMB")
True
What makes a valid burger?
There are 2 conditions to signify what makes a valid burger:
- starts and ends with a bun (use string operators/indexing;
[1]and[-1]should be good) - contains known ingredients (does a third bun in between two buns count as valid?)
Task 2
Write a function called burger_price(burger), which takes in a burger and returns the price of the burger.
A sample output is as follows:
Presentable Format
If we look at prices in many currency formats, they normally constitute a dollar-like value and cent-like value, both separated by a decimal point (.) in the same order.
One way we can replicate a 2-decimal-place like presentation of the price here is through a format modifier on a string:
We want burger_price() to return a float value and not a string, so we will not proceed to use this as part of this function's workflow.
However, this can come in handy if you were to create another function that acts like a print receipt one.
Task 3
Modify burger_price() from Task 2 to use a dictionary to keep track of which ingredients cost how much.
Depending on where you define your dictionary of ingredient costs, you may choose to modify is_burger() to depend on it for verifying valid burger ingredients.
Where to Put Your Dictionary
You may declare the dictionary within the local scope of burger_price(), or in a global scope.
Declaring it in the local scope of a function means that this dictionary only exists and thus can only be accessed from within that function itself, not out of it.
Declaring it in the global scope, however, would mean that typically the dictionary and its contents can be accessed throughout all the functions or just outside of all of them within the program file.
Task 4
Some children can be known to be picky eaters - whether this stems from parental upbringing (source here) or exposure to some kinds of foods (source here) is uncertain for sure for all cases.
Write a function called swap_ingredients(burger, old_ingredient, new_ingredient) which replaces every instance of old_ingredient with new_ingredient in burger.
The only constraint is that you are not allowed to swap out buns ('B') from the start and end of the burger string.
You are allowed to swap out buns ('B') that are in between, though.
If the operation is invalid (e.g., invalid ingredient) or the old ingredient does not exist, return the same burger string.
A sample output is as follows:
>>> print(swap_ingredients("BVPB", "V", "C"))
BCPB
>>> print(swap_ingredients("BVPB", "V", "A"))
BVPB
>>> print(swap_ingredients("BVPB", "C", "P"))
BVPB
>>> print(swap_ingredients("BVBPB", "B", "C"))
BVCPB
>>> print(swap_ingredients("BVPB", "B", "C"))
BVPB
Swapping Out the Correct Ingredients
Recall that a valid burger must be included within two sesame buns.
This effectively means that a valid burger string must start and end with a 'B', which by extension means that those two ingredients cannot be swapped out.
What can be swapped out though, is what is sandwiched in the middle.
Therefore, it would be a good idea to retrieve the substring burger[1:-1] and then look in there to see if any of its contents needs to be swapped out.
From here, you can then return that with a 'B' attached to both ends.
Suppose you created a new variable new_ingredients to contain the new burger's ingredients; you would probably return something like this at the end:
Task 5
As a result of swapping ingredients, an extra $0.10 is charged for the new ingredient.
For example, suppose for burger 'BVPB', the price is $0.50 + $0.70 + $1.50 + $0.50 = $3.20.
To swap out all veggies (V) with cheese (C), every new cheese ingredient costs $0.10 extra.
The new cost of this burger 'BCPB' becomes $0.50 + ($0.70 + $0.10) + $1.50 + $0.50 = $3.40.
Write a function called new_burger_price(burger, old_ingredient, new_ingredient) that returns the cost of the new burger after swapping ingredients.
A sample output is as follows:
Checking for Swapped Ingredients
Retrieving results from swap_ingredients() to use as part of the new_burger_price() function's procedure brings in a problem - you will need to compare the old burger against the new burger produced from swap_ingredients().
A better solution would be to just iterate through the burger string, and act accordingly when the old_ingredient is found.
Of course, considerations like the start and end of the burger string needing to be a 'B' must be taken into account.