Problems › Python › production
How many batches to run
The mixer takes a fixed number of units per batch, and a part-full batch still has to be run.
- A leftover of even one unit needs a whole extra batch.
- A batch size of zero or less makes no sense: return 0.
- Nothing to make means no batches.
batches_needed(units: int, per_batch: int) → int
Where you start
def batches_needed(units: int, per_batch: int) -> int:
Worked examples
| Call | Result |
|---|---|
batches_needed(100, 25) | 4 |
batches_needed(101, 25) | 5 |
batches_needed(1, 25) | 1 |
batches_needed(0, 25) | 0 |
Hint
Ceiling division in integers: (units + perBatch - 1) / perBatch.
Reference solution in Python
def batches_needed(units: int, per_batch: int) -> int:
if units <= 0 or per_batch <= 0:
return 0
return (units + per_batch - 1) // per_batch