Shift Pay Promotions
0:00:00
Let’s say we’re building payroll for hourly workers at Justworks. Write a function called calculate_pay that returns each worker’s total pay for completed shifts.
It takes:
- base_rates: a dict of each worker’s starting hourly rate.
- shifts: a list of (worker, start, end) tuples, where end is None if the worker is still clocked in.
- promotions: a list of (worker, time, new_rate) tuples, where new_rate replaces the worker’s hourly rate.
- double_pay: a list of (start, end) periods.
All times are hours since the start of the pay period.
Rules
- A shift is paid at the rate in effect when it started: a promotion at time t applies to shifts that start at or after t, never to a shift already in progress.
- A shift earns double only if it falls entirely inside a single double-pay period, bounds included. A shift that spans two periods earns normal pay, even if the periods touch or overlap.
- Every worker in base_rates appears in the output, with 0 if they have no completed shifts.
- Every worker in shifts and promotions is in base_rates, and every completed shift has start <= end.
- The lists can be in any order, and a worker never has two promotions at the same time.
Example:
Input:
base_rates = {"ana": 20, "ben": 30}
shifts = [("ana", 9, 17), ("ana", 30, 38), ("ben", 40, 45), ("ben", 50, None)]
promotions = [("ana", 20, 25)]
double_pay = [(40, 48)]
Output:
def calculate_pay(base_rates, shifts, promotions, double_pay) -> {"ana": 360, "ben": 300}
.
.
.
.
Comments