Travel expense conversion
MediumOnline Assessment
Convert the expenses of a business trip into US dollars for the expense report.
Each entry of rates is "<date> <currency> <rate>": on that date, one unit of the currency was worth rate US dollars. Each entry of expenses is "<date> <currency> <amount>": an amount paid in that currency on that date. Refunds have a negative amount. Dates are YYYY-MM-DD, in no particular order in either list.
To convert an expense in a currency other than USD:
- Take the rate for that currency with the latest date on or before the expense date. If several rates share that date, the one listed last in
ratesis used. - The rate is only trusted for 7 days: if it is dated more than 7 days before the expense (exactly 7 days is fine), or if no rate for that currency is dated on or before the expense, the expense cannot be converted and is skipped.
- The value is
amount * rateUS dollars, converted to whole cents and rounded to the nearest cent, with exact halves rounded away from zero (7.5 cents becomes 8, -7.5 cents becomes -8).
An expense in USD is already in dollars and needs no rate (any USD rates are ignored); its amount is converted to cents exactly. Round every expense separately, then add up the cents.
Input: rates and expenses, lists of strings in the format above.
Output: a list [total_cents, skipped]: the sum, in cents, of all converted expenses, and the number of expenses that could not be converted. For no expenses, return [0, 0].
Example 1
- Input:
- [["2024-03-01 EUR 1.0850","2024-03-05 EUR 1.0900","2024-03-01 GBP 1.2700"],["2024-03-01 EUR 10.00","2024-03-04 EUR 20.00","2024-03-05 EUR 100.00","2024-03-06 USD 5.25","2024-02-28 GBP 3.00"]]
- Output:
- [14680,1]
- Explanation
- An EUR expense uses the latest rate dated on or before the expense: 03-01 uses 1.0850, 03-04 still uses 1.0850, and 03-05 uses the rate dated that very day, 1.0900. The USD expense needs no rate. The GBP expense on 02-28 predates its only rate (dated 03-01), so it cannot be converted and is counted as skipped.
Example 2
- Input:
- [["2024-03-01 JPY 0.0070","2024-03-01 JPY 0.006712","2024-02-20 JPY 0.0100"],["2024-03-08 JPY 1500","2024-03-09 JPY 1500"]]
- Output:
- [1007,1]
- Explanation
- On 2024-03-01 there are two JPY quotes; the one listed later (0.006712) wins. 1500 JPY on 03-08 is converted: the rate is exactly 7 days old. On 03-09 it is 8 days old and too stale, so that expense is skipped. 1500 * 0.006712 = 10.068 USD, which is 1007 cents.
Example 3
- Input:
- [["2024-05-01 CAD 0.7500"],["2024-05-01 CAD 0.10","2024-05-02 CAD -0.10","2024-05-02 CAD 0.30","2024-05-03 CAD 0.10"]]
- Output:
- [31,0]
- Explanation
- At 0.7500, 0.10 CAD is 7.5 cents, which rounds up to 8; the refund of -0.10 CAD is -7.5 cents and rounds away from zero to -8; 0.30 CAD is 22.5 cents, so 23. Every expense is rounded on its own before adding: 8 - 8 + 23 + 8 = 31, whereas rounding the sum of the exact values would give 30.
Constraints
0 <= rates.length <= 200;0 <= expenses.length <= 200- Dates are valid
YYYY-MM-DDdates; currency codes are 3 capital letters; rates are positive decimals with at most 6 digits after the point; amounts are decimals (with an optional leading-) with at most 3 digits after the point, and|amount| <= 1000000