KatInterview

Scale Serving Sizes in a Shopping List

StringsAuto-graded · 20 test cases
EasyAuto-gradedStringsstringsparsinglist manipulation

Last updated

Tests careful string parsing: multiply only the leading numeric token in each line while leaving the rest of the text exactly as-is.

You have a shopping list written for a single person. Each line starts with a whole number representing how much of that item is needed, followed by exactly one space, then a description of the item.

Given the number of people to serve (nb_persons) and the list of ingredient lines, return a new list where every leading quantity has been multiplied by nb_persons. Everything else — the description text, its spacing, its punctuation, and any digits that appear inside it — must be carried over without modification.

Do not adjust singular or plural wording in the description. The multiplied quantity should be written as ordinary decimal digits with no leading zeros.

How Each Line Is Structured

<quantity> <description>
  • <quantity> — a positive integer, the first token on the line (before the first space).
  • <description> — everything after that first space, which may itself contain digits, spaces, or special characters. Treat it as opaque text.

Constraints

  • 1 <= nb_persons <= 1 000 000
  • 0 <= len(ingredients) <= 10 000
  • Each leading quantity is a decimal integer between 1 and 1 000 000 inclusive.
  • The description that follows is non-empty and contains at most 200 ASCII characters.
  • Every character after the first space belongs to the description and must be preserved exactly.
  • The maximum possible product (1 000 000 × 1 000 000 = 10^12) exceeds a signed 32-bit integer — use a type or language feature that handles this.

Examples

Example 1

Input: scale_ingredient_amounts(3, ['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']

Example 2

Input: scale_ingredient_amounts(4, ['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

The only number you should multiply is the one that comes before the very first space on each line. Any digits elsewhere are part of the item description and must not change.

Hint 2

Split each line at the first space only — not at every space. In Python, `str.split(' ', 1)` does exactly this: it gives you two parts and stops after the first split, so the rest of the description is untouched regardless of what it contains.

Hint 3

After scaling the leading number, reconstruct the line by joining the new quantity, a single space, and the original description string. Avoid using replace() or a regex that matches all digit runs — those approaches would also alter numbers embedded in the description.