Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Q: How much does it cost to bake a cake?
- Table Food
- +----------+-------+
- | Name | Price |
- +----------+-------+
- | Cake | 4 |
- | Egg | 8 |
- | Frosting | 15 |
- | Milk | 16 |
- | Sugar | 23 |
- +----------+-------+
- (price does not include the cost of ingredients)
- Table Recipe
- +----------+------------+----------+
- | Parent | Ingredient | Quantity |
- +----------+------------+----------+
- | Cake | Egg | 8 |
- | Cake | Frosting | 1 |
- | Cake | Milk | 1 |
- | Frosting | Egg | 2 |
- | Frosting | Milk | 0.5 |
- | Frosting | Sugar | 3 |
- +----------+------------+----------+
- Goal: given the name of a food, determine its total cost including cost of ingredients.
- Example 1: Frosting = (its own price) + 2 Egg + 1/2 Milk + 3 Sugar = 15 + 16 + 8 + 69 = 108$
- Example 2: Cake = (its own price) + 8 Egg + 1 Frosting + 1 Milk = 4 + 64 + 108 + 16 = 192$
- Notice that a nonrecursive solution can't calculate the total cost of cake, because you can't determine the total cost of Frosting that way.
- Hard mode: cache the results of previous total cost calculations, so you don't have to e.g. re-query Eggs more than once.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement