How many servings
- O(n) · Medium
- Full plan
- Python
- JavaScript
- lists
- division
- loops
Problem
The pantry at the scout camp is running low, and tonight it's chili.
The recipe says how much of each ingredient one serving takes, and whichever ingredient runs out first decides how many servings you get. Write a function that takes recipe and pantry, two lists of integers of the same length: recipe[i] is what one serving needs of ingredient i (it's never 0) and pantry[i] is how much of that ingredient you have (it can be 0). Return, as an integer, how many full servings you can make: half a serving doesn't count. If both lists are empty, return 0.
With recipe = [200, 3, 50] and pantry = [1000, 10, 120]: the first covers 5 servings, the second 3 and the third 2. You get 2.
Examples
The one from the example
[200, 3, 50], [1000, 10, 120] → 2
Short from the first one
[2, 1], [1, 100] → 0
A single ingredient, with some left over
[4], [19] → 4
The last one decides
[1, 2, 3, 5], [9, 9, 9, 12] → 2
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def servings(recipe, pantry):
passJavaScript
function servings(recipe, pantry) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.