Python traps · Chapter 5 of 16

Integer division and round()

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Everyday traps
  • 3 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python traps are free. This one comes with the all labs plan.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.py: a fee and a tip pool that come out short

    Write reproduce() in repro.py. From payouts.py, take rider_fee(2.5), the fee for a 2.5 km run (20 + 5 × 2.5 = 32.5), and split a ₹100 tip pool among ["Santosh", "Imran", "Prakash"] with split_tip and add up the shares it gives. Return both as a list: [the fee, the sum of the shares].

    The check runs your reproduce() twice: beside the original payouts.py it must return [32, 99] (the half rounded down, a rupee of the pool lost), and beside a fixed payouts.py it must return [33, 100]. So it must really call rider_fee and split_tip. No real waiting and no input(): reproduce() must finish within 3 seconds.

  2. Task 2

    payouts.py: fees round half up, and tips add up

    Fix payouts.py. rider_fee(km) gives 20 + 5 × km, for km as written, as a whole number of rupees (an int), with half a rupee or more going up and less going down: 2.5 km (32.5) is 33, 3.3 km (36.5) is 37, 2.04 km (30.2) is 30, 2.18 km (30.9) is 31. split_tip(total, riders) gives a dict with every rider, in the list's order, and a whole number of rupees (an int) each; the shares add up to the total exactly, and when the total does not split evenly, the rupees left over go one each to the first riders: 100 among three is 34, 33, 33; 200 among three is 67, 67, 66; 0 gives every rider 0.

    The check imports a fresh copy of your payouts.py and tries many fees, halves and not, and many totals and rider counts. A Decimal made straight from a float km keeps the float's error (4.1 km must give 41), and rounding every fee up is as wrong as rounding halves down.

  3. Task 3

    test_payouts.py: a test that would have caught it

    Write a pytest file, test_payouts.py, that would have caught this bug before the riders noticed. The check runs your file three times: beside the original payouts.py, where at least one test must fail; beside the fixed payouts.py, where every test must pass; and beside a wrong fix that rounds every fee up with math.ceil, where at least one test must fail.

    So test both sides of the fee: a fee that ends in .5 goes up (rider_fee(2.5) is 33), and a fee that is not a half goes to the nearest rupee (rider_fee(2.04) is 30). Test that a tip split adds up to its pool too. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.py: a fee and a tip pool that come out short
  • payouts.py: fees round half up, and tips add up
  • test_payouts.py: a test that would have caught it

More in Everyday traps

Build "Integer division and round()" yourself

DabbaDrop runs on Python, and Kunal, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python traps are free. This one comes with the all labs plan.