bhylu.com

Advertisements

Code Challenge

Start with seven tasks and process at most two per pass until none remain. Print the remainder after each pass.

Understand the idea

Write your own solution before opening the worked answer. Predict the output from the supplied values, then compare the actual result. Check both an ordinary case and the boundary described below; a passing example is more useful when you can explain why it passes.

Where to try it

Open this example with Try in playground. The code loads as editable text and does not run until you select Run. The first run downloads Python. Each run starts fresh; temporary files created by a script are not files on your PC.

Your task

Start with seven tasks and process at most two per pass until none remain. Print the remainder after each pass.

Show the worked solution
Python

remaining = 7
while remaining > 0:
    remaining = max(0, remaining - 2)
    print(remaining)

Check the result

The remainders are 5, 3, 1, and 0.

Test one more case

Change the batch size and verify that the remainder never becomes negative.