Python traps 2 · Chapter 5 of 16

Sorting by two keys, one reversed

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

  • Part: Names
  • 3 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python traps 2 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: the 1 pm corporate order above the 12 pm one

    Write reproduce() in repro.py. Make three orders, each a dict with "id", "priority" and "slot": D3143 with priority 1 (corporate) and slot "13:00", D3144 with priority 1 and slot "12:00", and D3142 with priority 0 and slot "11:30". Give them to kitchen_queue from kitchen.py and return the list of ids it gives.

    The check runs your reproduce() twice: beside the original kitchen.py it must return ['D3143', 'D3144', 'D3142'] (the 1 pm order above the 12 pm one), and beside a fixed kitchen.py it must return ['D3144', 'D3143', 'D3142']. So it must really call kitchen_queue with those three orders. No real waiting and no input(): reproduce() must finish within 3 seconds.

  2. Task 2

    kitchen.py: the earliest corporate slot goes first

    Fix kitchen_queue in kitchen.py. It returns the orders' ids sorted by priority, higher first (any whole numbers: 1 corporate, 0 the rest), then by slot, earlier first ("HH:MM", 24 hours, zero padded), then by order id, lower first. It returns a new list and leaves the list it was given as it was. next_batch(orders, n) gives the first n ids of that queue, or all of them when there are fewer.

    Keep the log line, kitchen: next batch <ids, one space between>, printed once per next_batch. The check imports a fresh copy of your kitchen.py and uses its own orders, with ordinary orders whose slot is earlier than a corporate one's: a queue that puts every early slot first, whatever its priority, is as wrong as the bug.

  3. Task 3

    test_kitchen.py: a test that would have caught it

    Write a pytest file, test_kitchen.py, that would have caught this bug before the 12 pm batch left late. The check runs your file three times: beside the original kitchen.py, where at least one test must fail; beside the fixed kitchen.py, where every test must pass; and beside a wrong fix that sorts twice in the wrong order, so the queue ends up by slot alone, where at least one test must fail.

    So use orders where the keys pull different ways: two corporate orders in different slots, and an ordinary order whose slot is earlier than a corporate one's, and check the whole queue. No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.py: the 1 pm corporate order above the 12 pm one
  • kitchen.py: the earliest corporate slot goes first
  • test_kitchen.py: a test that would have caught it

More in Names

Build "Sorting by two keys, one reversed" yourself

Monsoon season at DabbaDrop brings a new pile of tickets from Kunal. 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 2 are free. This one comes with the all labs plan.