Checkout with three-for-two deals
An online store in Amsterdam builds its basket engine. Prices are stored in cents (integers) to avoid rounding trouble. The input has three sections.
- Catalog:
C, thenClinesSKU,name,price_cents. - Cart events:
E, thenElinesADD SKU qtyorREMOVE SKU qty. Unknown SKUs are ignored. A removal never takes the quantity below 0. - Promotions:
P, thenPlines, either3FOR2 SKU(every complete group of 3 units of that SKU gives one unit free, i.e.qty // 3free units; repeating the same line changes nothing) orCOUPON pct(an integer percent; if several coupon lines appear, only the last counts).
Money is printed as euros.cents with two digits for cents (for example 5 cents = 0.05). The coupon discount is (subtotal - promo savings) * pct // 100 (rounded down to whole cents).
Task: print one line per SKU with quantity above 0, as SKU xQTY gross where gross = qty * price, sorted by gross descending then SKU ascending. Then print Subtotal: ... (sum of gross), Promo savings: ..., Coupon: ... and Total: ... (subtotal minus savings minus coupon). If the cart is empty print only EMPTY CART.
Sample tests
Input
4 TEA,Green tea,499 MUG,Mug,850 CAKE,Stroopwafel pack,325 PEN,Pen,99 7 ADD TEA 3 ADD MUG 1 ADD CAKE 7 REMOVE CAKE 1 ADD GHOST 5 ADD PEN 2 REMOVE MUG 1 2 3FOR2 CAKE COUPON 10
Expected output
CAKE x6 19.50 TEA x3 14.97 PEN x2 1.98 Subtotal: 36.45 Promo savings: 6.50 Coupon: 2.99 Total: 26.96
Input
1 A,Alpha,100 3 ADD A 1 REMOVE A 5 ADD B 2 0
Expected output
EMPTY CART
Your answer is also checked against 3 hidden tests.
Log in or create a free account to run your code, submit and earn points.
Run your code to see the output here. Ctrl + Enter also runs it.
