Till receipt
EasyOnline Assessment
Rebuild what each item brought in from the lines a café till printed during the day.
Every line is in one of these formats:
NAME @ PRICEsells one item.NAME xQ @ PRICEsellsQof them (Qis a positive integer).PRICEis the price of one item written as digits, a dot and exactly two digits (4.50).- Either sale format may end with a discount, written as a space and then
-N%(Nis an integer from 1 to 100). The discount applies to the whole line. VOID NAMEcancels the most recent sale line forNAMEthat has not been cancelled yet. If there is no such line, theVOIDis ignored.
A name contains only letters and single spaces, it never starts with VOID, and it is case-sensitive.
A sale line is worth Q * price in cents. With a discount of N%, the line is worth (Q * price_in_cents * (100 - N)) divided by 100 and rounded to the nearest cent, with exact halves rounded up. A cancelled line is worth nothing and does not count at all.
Input: lines, a list of strings in the formats above.
Output: a list with one entry [name, cents] for every name that has at least one sale line that was not cancelled, where cents is the sum of those lines' values. List the names in the order in which their first surviving (not cancelled) sale line appears in lines. If nothing survives, return [].
Example 1
- Input:
- [["Latte x2 @ 4.50","Bagel @ 3.25","Latte @ 4.50"]]
- Output:
- [["Latte",1350],["Bagel",325]]
- Explanation
- "Latte x2 @ 4.50" is 900 cents, "Bagel @ 3.25" is 325, and the last Latte is another 450. Lattes total 1350. Names are listed in the order of their first line, so Latte comes before Bagel.
Example 2
- Input:
- [["Muffin @ 2.75","Tea @ 2.00","Muffin x3 @ 2.75 -20%","VOID Muffin","VOID Scone"]]
- Output:
- [["Muffin",275],["Tea",200]]
- Explanation
- The 20% discount makes the 3-muffin line 3*275*80/100 = 660. "VOID Muffin" cancels the most recent muffin line (the 660 one), not the first, so Muffin ends at 275. "VOID Scone" has nothing to cancel and is ignored. Tea is listed after Muffin because its first line comes later.
Example 3
- Input:
- [["Cake @ 5.00","Tea @ 2.00","VOID Cake","Latte x3 @ 1.05 -50%","Cake @ 5.00"]]
- Output:
- [["Tea",200],["Latte",158],["Cake",500]]
- Explanation
- The first Cake line is cancelled by the void, so Cake's first surviving line is the last one in the list and Cake is listed last, after Tea and Latte. Latte x3 @ 1.05 with -50% is 315*50/100 = 157.5, which rounds up to 158.
Constraints
0 <= lines.length <= 2001 <= Q <= 99; prices are between0.01and99.99- Names are 1 to 20 letters and spaces; no name starts with
VOID