Technical skill practice
Python Modulo: Assign a Cyclic Roster with Signed Offsets
Python’s % operator gives the remainder associated with floor division. With a positive divisor, the result can wrap integer offsets into valid positions in a repeating cycle. Negative offsets wrap too, which is useful when moving backward through a roster.
Start with a small, explicit task
Assign positions in a three-person fictional roster for offsets before and after the starting point. The roster repeats indefinitely as a mathematical model, but the script checks only a fixed finite set. It does not schedule real staff or determine fair workload distribution.
Run the complete example
This original example was checked with Python 3.12.14. Save it as python_modulo.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.
Expected output
[1, 2, 0, 1, 2, 0, 1]
['Bo', 'Cy', 'Ari', 'Bo', 'Cy', 'Ari', 'Bo']
2 -2
roster = ["Ari", "Bo", "Cy"]
offsets = [-2, -1, 0, 1, 2, 3, 4]
indexes = [offset % len(roster) for offset in offsets]
assigned = [roster[index] for index in indexes]
assert indexes == [1, 2, 0, 1, 2, 0, 1]
assert assigned == ["Bo", "Cy", "Ari", "Bo", "Cy", "Ari", "Bo"]
assert -1 % 3 == 2 and 1 % -3 == -2
assert -1 == (-1 // 3) * 3 + (-1 % 3)
print(indexes)
print(assigned)
print(-1 % 3, 1 % -3)
Explain the result
Taking each offset modulo three gives only 0, 1 or 2, so every resulting index selects a roster member. Offset three wraps to the same position as zero, while offset -1 selects the preceding position, 2. This allows a cyclic rule without building repeated copies of the roster.
The sign comparison shows why assumptions from another language can be misleading: -1 % 3 is 2 here, while 1 % -3 is -2. The remainder is consistent with the floor-division quotient, as the final identity assertion checks. Use the positive roster length as the divisor for this indexing task.
Avoid these mistakes
An empty roster has length zero and cannot be used as the divisor; decide how that input should be rejected before assigning work. Also distinguish remainder from percentage formatting: the operator here is arithmetic, and the inputs are deliberately integers.
Questions, answered
Why does a negative offset produce a positive index?
With the positive divisor three, the checked remainders lie within the roster’s valid index range.
Does modulo make a schedule fair?
It supplies a repeating position rule only. Availability, workload and fairness constraints need their own model.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.