Recipe card calories
EasyOnline Assessment
Work out the calorie figure printed on a recipe card.
A recipe lists ingredients as [name, amount, unit], where amount is a positive integer and unit is one of g, kg, ml, l, tsp, tbsp, cup. Volumes convert exactly: 1 tsp = 5 ml, 1 tbsp = 15 ml, 1 cup = 240 ml, 1 l = 1000 ml. 1 kg = 1000 g.
A facts table gives, for some ingredients, [name, kcal_per_100g, grams_per_100ml] (both integers). To get the weight of an ingredient listed by volume, use its density: grams = ml * grams_per_100ml / 100. Ingredients listed by weight need no density. Names match exactly, and the same ingredient may appear on several lines. An ingredient that is not in the facts table (water, salt, ...) contributes 0 kcal.
Add up the exact calories of all lines: grams * kcal_per_100g / 100 for each, without rounding anything along the way. Divide by servings to get the exact calories per serving. The card prints this value rounded to the nearest multiple of 5; a value exactly halfway between two multiples (for example 12.5) rounds up.
Input: ingredients, a list of [name, amount, unit]; facts, a list of [name, kcal_per_100g, grams_per_100ml]; and servings, a positive integer.
Output: the printed calories per serving, an integer.
Example 1
- Input:
- [[["flour",2,"cup"],["butter",100,"g"],["water",250,"ml"]],[["flour",364,53],["butter",717,95]],4]
- Output:
- 410
- Explanation
- 2 cups of flour is 480 ml, which weighs 480 * 53 / 100 = 254.4 g and holds 254.4 * 364 / 100 = 926.016 kcal. The butter adds 717 kcal. Water is not in the facts table, so it adds nothing. The total is 1643.016 kcal, or 410.754 per serving of 4, which rounds to 410 (the nearest multiple of 5).
Example 2
- Input:
- [[["milk",1,"l"],["honey",2,"tbsp"],["honey",1,"tsp"]],[["milk",64,103],["honey",304,142]],2]
- Output:
- 405
- Explanation
- 1 l of milk is 1000 ml = 1030 g = 659.2 kcal. 2 tbsp of honey is 30 ml = 42.6 g = 129.504 kcal and 1 tsp is 5 ml = 7.1 g = 21.584 kcal; the two honey lines are both counted. The total is 810.288, or 405.144 per serving of 2, which rounds to 405.
Example 3
- Input:
- [[["rice",1,"kg"]],[["rice",130,85]],8]
- Output:
- 165
- Explanation
- 1 kg of rice is 1000 g, so no density is needed: 1300 kcal. Split over 8 servings that is exactly 162.5, halfway between 160 and 165, so it rounds up to 165.
Constraints
0 <= ingredients.length <= 40,0 <= facts.length <= 101 <= amount <= 400,1 <= servings <= 20kcal_per_100gandgrams_per_100mlare integers from 1 to 900- Names are 1 to 12 letters and are compared exactly (case-sensitive)