Lesson 4-3 · The Planner: Every Direction
Lesson 4-3 · The Planner: Every Direction
Teacher mode is on. Toggle it off (bottom-right) to preview the student view.
Last lesson's delivery run fell apart on Leg 2: the planner can only go right and up. Today you finish it. First you'll write tests that say exactly what's wrong, then add the two missing loops and watch the tests turn to PASS.
Learning Objectives
By the end of this lesson you will be able to:
- Compare two tuples, or two lists, with
== - Test a function with
run_test()against paths you traced by hand - Write the west and south loops, with no
if/else - State the planner's three promises
Activity · Make your answer key
Trace five paths by hand: across first, then up or down. This is your answer key: the tests compare the code with it. Leg 1 you traced last lesson; Leg 2 is the one that came out empty.
| Test | From, to | Expected path |
|---|---|---|
| Leg 1 | (0, 0) to (2, 3) | |
| Leg 2 | (2, 3) to (1, 0) | |
| Same column | (2, 0) to (2, 3) | |
| Same row | (0, 2) to (3, 2) | |
| Same spot | (1, 1) to (1, 1) |
What is different about Leg 2 compared with Leg 1?
Let the computer compare
Last lesson you checked three printed paths against your traces by eye. With five paths,
and a re-run after every change, that gets slow and easy to get wrong. Python can compare
for you. == works on tuples and on lists:
print((1, 2) == (1, 2)) # True
print((1, 2) == (2, 1)) # False -- order matters
print([(1, 0), (2, 0)] == [(1, 0), (2, 0)]) # True
print([(1, 0), (2, 0)] == [(1, 0)]) # False -- one is shorter
Two tuples are equal when both parts match, in order. Two lists are equal when they hold
equal items, in the same order, and the same number of them. So one == checks a whole
path.
Does (1, 3) == (3, 1) evaluate to True?
This lesson's skeleton
Add run_test() below the compute_manhattan_path() function, and replace last
lesson's test_planner() with this one. Add the two new comments inside
compute_manhattan_path(): west goes between east and north, so the path still goes
across first.
# West: copy east, flip < to > and + to -
# (goes between the east loop and the north loop)
# South: copy north, flip < to > and + to -
# (goes after the north loop, before return path)
def run_test(test_name, start, destination, expected):
"""Plan start -> destination and compare the result
with expected; print test_name with PASS or FAIL."""
actual = compute_manhattan_path(start, destination)
if actual == expected:
print(test_name, "- PASS")
else:
print(test_name, "- FAIL")
print(" Expected:", expected)
print(" Actual: ", actual)
def test_planner():
# expected = your answer key, traced by hand
run_test("Leg 1", (0, 0), (2, 3), [(1, 0), (2, 0), (2, 1), (2, 2), (2, 3)])
run_test("Leg 2", (2, 3), (1, 0), []) # your answer key
run_test("Same column", (2, 0), (2, 3), []) # your answer key
run_test("Same row", (0, 2), (3, 2), []) # your answer key
run_test("Same spot", (1, 1), (1, 1), []) # your answer key
The expected lists must come from your own tracing. Copy what the code prints and the test only checks that the code agrees with itself.
Activity · Finish the planner
Step 1 · Run the tests first
Replace each [] in test_planner() with the path from your answer key. (Same spot
stays [].) Set the run line to test_planner() and run it, before you change the
planner.
What correct looks like: four PASS lines and one FAIL, each under the planner's own
computing path line. The test names the problem exactly:
computing path from: (0, 0) to: (2, 3)
Leg 1 - PASS
computing path from: (2, 3) to: (1, 0)
Leg 2 - FAIL
Expected: [(1, 3), (1, 2), (1, 1), (1, 0)]
Actual: []
computing path from: (2, 0) to: (2, 3)
Same column - PASS
computing path from: (0, 2) to: (3, 2)
Same row - PASS
computing path from: (1, 1) to: (1, 1)
Same spot - PASS
Step 2 · West: copy and flip
West is the east loop flipped: copy it under the west comment, then flip < to > and
+ 1 to - 1. Run test_planner(): Leg 2 still fails, but Actual: is now [(1, 3)].
The step left is there; the steps down are not.
Why two x loops can sit next to each other: a while loop checks its condition before
its first trip. Going left, x is already bigger than dest_x, so the east loop's
x < dest_x is False on that first check and the east loop never runs. Only the west
loop moves x.
Step 3 · South
South is the north loop, flipped the same way. Run test_planner(): five PASS lines.
Same column, same row and same spot need no code of their own: a loop with nothing to do
never runs.
For a trip that goes up and to the right, how many of the four while loops actually run?
Step 4 · Run the main program
Set the run line back to test_delivery() and run it.
What correct looks like: Leg 1 and Leg 2 match your answer key. In every leg, the path starts one step from where the leg began, goes across first, and ends at the destination. The driver is still a stub, so the heading is still 0.
The planner's three promises
The planner is finished, and it makes three promises. Every planner in this course keeps them:
- The path never includes the start. The robot is already there.
- The path ends at the destination.
- The path is empty if the robot is already at the destination.
When a test fails
Don't guess: add a print inside compute_manhattan_path() to watch it run, in each loop
right after the append:
print(" added:", (x, y))
The output shows which loop ran and what it added. Once the bug is fixed, remove the
debug prints so the output stays clean. Try it: change one + 1 to - 1, run
test_planner(), watch a test catch it, then change it back.
A classmate gets [(3, 3), (2, 3), (1, 2), (1, 1), (1, 0)] for (3, 3) to (1, 0). What went wrong?
Wrap-up
- Why four while loops instead of
if/else? (A loop whose condition starts False never runs, so only the right ones do.) - When are two paths equal with
==? (Same positions, in the same order, and the same number of them.) - What are the planner's three promises? (No start in the path; it ends at the destination;
[]when already there.) - Where does a test's expected path come from? (Your own hand trace, before you run the code.)