Scale Recipe Quantities Without Changing Product Descriptions
UpstartSoftware Engineer · Online AssessmentLast updated
Multiply only the leading quantity in each ingredient line, preserving recipe order, product wording, and numbers inside descriptions.
Scale a one-person recipe for nb_persons people. Each ingredient line begins with a positive integer quantity, followed by one space and a product description. Multiply only the leading quantity; preserve the description and line order.
Function Contract
Implement adjust_quantities(nb_persons, ingredients) -> list[str].
Do not change singular or plural wording. Numbers inside the product description are part of that description and must remain unchanged.
Constraints and Clarifications
The numeric and size bounds below are explicit practice bounds for the stated format.
1 <= nb_persons <= 1000000.0 <= len(ingredients) <= 10000.- Each leading quantity is an ASCII decimal integer from
1through1000000. - The first space separates that quantity from a nonempty product description of at most 200 ASCII characters.
- Preserve every character after that first space, including other digits and spaces.
- Format the multiplied quantity as ordinary decimal digits without unnecessary leading zeros.
- Intermediate products may exceed a signed 32-bit integer.
Examples
nb_persons = 3
ingredients = ["2 eggs", "200 grams of flour", "150 grams of sugar", "1 liter(s) of milk"]
Output: ["6 eggs", "600 grams of flour", "450 grams of sugar", "3 liter(s) of milk"]
nb_persons = 4
ingredients = ["2 packs of 6 rolls", "1 bottle of 2 percent milk"]
Output: ["8 packs of 6 rolls", "4 bottle of 2 percent milk"]
Hints
Hint 1
Split only at the quantity boundary: The first space identifies the numeric prefix. Parsing every number in a line would also change numbers belonging to the product description.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.