Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2021
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. Q: How much does it cost to bake a cake?
  2.  
  3. Table Food
  4. +----------+-------+
  5. | Name | Price |
  6. +----------+-------+
  7. | Cake | 4 |
  8. | Egg | 8 |
  9. | Frosting | 15 |
  10. | Milk | 16 |
  11. | Sugar | 23 |
  12. +----------+-------+
  13. (price does not include the cost of ingredients)
  14.  
  15.  
  16. Table Recipe
  17. +----------+------------+----------+
  18. | Parent | Ingredient | Quantity |
  19. +----------+------------+----------+
  20. | Cake | Egg | 8 |
  21. | Cake | Frosting | 1 |
  22. | Cake | Milk | 1 |
  23. | Frosting | Egg | 2 |
  24. | Frosting | Milk | 0.5 |
  25. | Frosting | Sugar | 3 |
  26. +----------+------------+----------+
  27.  
  28. Goal: given the name of a food, determine its total cost including cost of ingredients.
  29. Example 1: Frosting = (its own price) + 2 Egg + 1/2 Milk + 3 Sugar = 15 + 16 + 8 + 69 = 108$
  30. Example 2: Cake = (its own price) + 8 Egg + 1 Frosting + 1 Milk = 4 + 64 + 108 + 16 = 192$
  31.  
  32. 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.
  33.  
  34. 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