Problem 182280 · hard · Phase 01 Prerequisites & Setup

A Receipt That Lines Up

input · strings · lists · arithmetic · f-strings · rounding

Print a grocery receipt whose amounts line up on the right. The first input line is n, the number of items. Each of the next n lines is

<quantity> <item name> @ <unit price>

The item name may contain spaces and digits but never @. The last line is the sales tax rate in percent (for example 8.875).

Every row has a left text and a money text:

  • item rows: left text is the name, followed by x<quantity> when the quantity is more than 1; money is quantity times unit price;
  • then Subtotal (sum of item rows), Tax (<rate>%) with the rate exactly as typed, and Total.

Tax is the subtotal times the rate, rounded to the nearest cent, halves rounded up. Money is written $ + dollars with comma thousands separators + . + two-digit cents.

Let L be the length of the longest left text and M the length of the longest money text (over all rows). Each row is its left text padded with spaces to L characters, two spaces, then its money text right-aligned in M characters. Between the last item and Subtotal print a line of L + 2 + M dashes.

Examples

Input:
3
2 Oat milk 1L @ 2.49
1 Sourdough loaf @ 6
12 Eggs (free range) @ 0.35
8.875
Output:
Oat milk 1L x2          $4.98
Sourdough loaf          $6.00
Eggs (free range) x12   $4.20
-----------------------------
Subtotal               $15.18
Tax (8.875%)            $1.35
Total                  $16.53

Constraints

  • 1 <= n <= 20, 1 <= quantity <= 999, unit prices up to 99999.99 with 0-2 decimals
  • the rate is between 0 and 30 with 0-3 decimals; no line has surrounding spaces

Goals

  • Parse decimal prices and a tax rate into exact whole numbers of cents
  • Round half-up to the cent with integer arithmetic instead of floats
  • Compute column widths from the data before printing any line
Starting Python…