Interleave Volunteers by Shift Type
Problem
A community kitchen tracks its volunteers and which shift type each one prefers. The DataFrame `roster` (loaded from `roster.csv`) has columns `(volunteer_id, shift)` where `shift` is one of `'morning'`, `'evening'`, or `'overnight'`.
Reorder the rows so the shifts cycle in the fixed order `'morning'`, then `'evening'`, then `'overnight'`, repeating. Inside each shift, list volunteers by `volunteer_id` ascending. So the first three rows are the lowest-id morning, evening, and overnight volunteers; the next three are the second-lowest of each shift; and so on. If a shift runs out of volunteers, simply skip it for the remaining cycles.
Return the columns `volunteer_id` and `shift` in this interleaved order.
Input data
Example rows — the live problem includes the full dataset.
| volunteer_id | shift |
|---|---|
| 4 | overnight |
| 7 | morning |
| 2 | evening |
| 5 | overnight |
| 3 | morning |
Expected output
Your answer should return 9 rows with the columns volunteer_id, shift.
Starter code (Pandas (Python))
import pandas as pd
def interleave_volunteers_by_shift(roster) -> pd.DataFrame:
# Your code here
return rosterSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock