Space Python Loading Python…

Shift rota

Write the codeHard 2 stages

The station runs around the clock: a morning, an afternoon and a night shift, every day of the week. Making the rota by hand takes the commander an hour every Sunday, and twice this month somebody ended up working a night shift straight after an afternoon.

Let the program build the rota, and check its own work.

Build rota, a list of (day, shift, name) tuples for every shift of the week, in order, taking the crew in turn. Print one line per day, then count how many times one person works two shifts in a row:

Mon    Morning: Kowalska    Afternoon: Tanaka      Night: Okafor
Tue    Morning: Bondarenko  Afternoon: Kowalska    Night: Tanaka
Wed    Morning: Okafor      Afternoon: Bondarenko  Night: Kowalska
Thu    Morning: Tanaka      Afternoon: Okafor      Night: Bondarenko
Fri    Morning: Kowalska    Afternoon: Tanaka      Night: Okafor
Sat    Morning: Bondarenko  Afternoon: Kowalska    Night: Tanaka
Sun    Morning: Okafor      Afternoon: Bondarenko  Night: Kowalska
Back-to-back shifts: 0

Count the shifts in slot as you go: crew[slot % len(crew)] wraps round to the first person after the last. {name:<10} pads the names into columns.

The week
A valid rota

Finish stage 1 to unlock this stage.

Now some of the crew have days off. Taking turns no longer works, so use a rule for each shift: of the people who aren't off that day and didn't work the shift before, choose the one with the fewest shifts so far, and on a tie the first in crew. Print the rota, the back-to-back check and the shifts per person:

Mon    Morning: Kowalska    Afternoon: Tanaka      Night: Okafor
Tue    Morning: Bondarenko  Afternoon: Kowalska    Night: Tanaka
Wed    Morning: Okafor      Afternoon: Bondarenko  Night: Kowalska
Thu    Morning: Tanaka      Afternoon: Okafor      Night: Bondarenko
Fri    Morning: Kowalska    Afternoon: Tanaka      Night: Okafor
Sat    Morning: Bondarenko  Afternoon: Kowalska    Night: Tanaka
Sun    Morning: Bondarenko  Afternoon: Kowalska    Night: Tanaka
Back-to-back shifts: 0
Shifts per person:
  Kowalska: 6
  Tanaka: 6
  Okafor: 4
  Bondarenko: 5

Keep the count in a dict, shifts_done, and the last person on duty in previous.

A fair week
Every rule kept
You're not logged in, so your progress is kept in this browser only. Log in to save it, and to use hints and solutions.

Hints

For Loop with enumerate and range

for-loop-enumerate-range.py
# range() produces a run of numbers so you do not have to type them out
print(list(range(5)))

# One argument: starts at zero, stops before the number you gave
for orbit in range(3):
    print("Orbit", orbit, "complete")

# Two arguments: a start and a stop. The stop is never included
for day in range(1, 6):
    print("Flight day", day)

# Three arguments: the last one is the step, and it can go backwards
for altitude in range(100, 0, -25):
    print(altitude, "km above the surface")

# enumerate() hands you the position and the item at the same time
checklist = ["hatch sealed", "fuel loaded", "guidance online", "range clear"]
for position, task in enumerate(checklist):
    print(position, task)

# start=1 numbers things the way a human would
for position, task in enumerate(checklist, start=1):
    print(f"Step {position}: {task}")

# Without enumerate you would be counting by hand
count = 0
for task in checklist:
    count = count + 1
print("Items checked:", count)
Open the full example
script.py
PythonLn 1, Col 1Spaces: 4
Loading the Python environment…

Separate arguments with commas. They're passed on every Run as sys.argv[1:].