Python traps 2 · Chapter 6 of 16
zip stops at the shortest list
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.
- Task 1
repro.py: three orders, two riders, and no error
Write reproduce() in repro.py. Make three orders, each a dict with an "id" (D3238, D3239 and D3240), and a rota of two riders, "Vikas Yadav" and "Sunil Gaikwad". Call assign from dispatch.py with them and return how many pairs it gave. If it raises, catch the error and return the name of its type, type(error).__name__, instead.
The check runs your reproduce() twice: beside the original dispatch.py it must return 2 (one order dropped without a word), and beside a fixed dispatch.py it must return "ValueError". So it must really call assign with three orders and two riders: a repro that just returns 2 fails the second run. No real waiting and no input(): reproduce() must finish within 3 seconds.
- Task 2
dispatch.py: every order gets a rider, or nothing goes
Fix assign in dispatch.py. With as many orders as riders, assign(orders, riders) returns a list of (order id, rider) tuples, the first order with the first rider and so on, in order; no orders and no riders give an empty list. When the lengths differ, fewer riders than orders or more riders than orders, it raises ValueError, and nothing is assigned. dispatch(orders, riders) assigns, prints one line per pair and a last line, and returns the pairs; when assign raises, the ValueError reaches dispatch's caller and nothing is printed.
Keep the log lines, dispatch <order id> -> <rider> and dispatch: <n> orders dispatched. The check imports a fresh copy of your dispatch.py and uses its own orders and riders, more riders than orders too: a fix that pairs an order with a rider of None and raises nothing is as wrong as the bug.
- Task 3
test_dispatch.py: a test that would have caught it
Write a pytest file, test_dispatch.py, that would have caught this bug before three Powai customers got no lunch. The check runs your file three times: beside the original dispatch.py, where at least one test must fail; beside the fixed dispatch.py, where every test must pass; and beside a wrong fix that uses itertools.zip_longest (an order gets the rider None, and no error), where at least one test must fail.
So test both sides: equal lists give the right pairs in order, and fewer riders than orders raises ValueError, checked with pytest.raises. No real waiting: each test must finish within 3 seconds.
What you will learn
- repro.py: three orders, two riders, and no error
- dispatch.py: every order gets a rider, or nothing goes
- test_dispatch.py: a test that would have caught it
More in Names
Build "zip stops at the shortest list" 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.