Today: function-call, decomposition, call-the-helper, Python style

What is "Done" For a Homework?

At the start, your homework code is not working. You work and iterate, and eventually it's working. Ideally you understand why every line is in there. Not like, it's working but you're not sure exactly how! The later exam problems will greatly resemble the homework problems, so there's an extra incentive to feel confident about your homework solutions.

Homeworks Take a Few Hours

The HW problems are not that easy. Can take hours. It's not just you! Looking at the code when it's correct, you can imagine that everyone else just typed it in correct right away, but in fact most students are spending the hours just like you.

The system has an internal logic, and you need to work through some of these points yourself to absorb how they work. When you are done with homework-1, go back and look at the early parts of it. Hopefully they now look kind of easy - you have learned that bit.

But we hope, once you have it working, you understand it, then could do it more easily. Like if your homework were deleted, you could do it again pretty readily.

Coding Style 1.0 - PEP8

CS106A doe not just teach coding. It has always taught how to write clean code with good style. Your section leader will talk with you about the correctness of code, but also pointers for good style.

All the code we show you will follow PEP8 which is the Python standard for superficial things like spacing and whatnot.

See our Python Guide Style section. We'll pick out a few things today (for hw1), re-visit it for the rest later.


Can We bit.move() Any Old Time?

The answer is no.

If there is a wall or black square in front of bit, then the move will fail with an error.

...
bit.move()


alt: bit error moving into a wall

Check bit.front_clear() before Move

Each move should be preceded by a front_clear() check - in effect this is how bit looks where it is going. This is the issue with this next problem.

Tricky Case: Double Move

> double-move (while + two moves in loop)

The goal here is that bit paints the 2nd, 4th, etc. moved-to squares red, leaving the others blank. This can be solved with two moves and one paint inside the loop, but it's a little tricky.


alt: double move output

The code below is a good first try, but it generates a move error for certain world widths. Why? The first move in the loop is safe, but the second will make an error if the world is even width. Run with Case-1 and Case-2 to see this.

def double_move(filename):
    bit = Bit(filename)
    while bit.front_clear():
        bit.move()
        bit.move()   # possible error
        bit.paint('red')

Usually you run your code one case at a time, using the Run All option as a final check that all the cases work. In this case, Run All reveals that some cases work, and some cases expose a bug in this code.

Aside: Do Not Run-All To Debug Code

To figure out what is wrong with your code and fix, run a single case so the lines hilight as it runs. Use Run-All to find a case with a problem, but don't debug with it.

Double Move Solution

The problem is the second move. It is not guarded by a front_clear() check, so depending on the world width, it will try move through a wall. The first move in the loop does not have this problem — think about the while-test just before it.

So the first move in the loop is save, but we don't know if the second move is safe or not. The solution is to add an if-statement that checks if the front is clear for the second move, only doing the move if the front is clear.

def double_move(filename):
    bit = Bit(filename)
    while bit.front_clear():
        bit.move()  # This move is fine
        if bit.front_clear():
            bit.move()  # This move needs a check
            bit.paint('red')

More Practice: Falling Water

The Falling Water problem in the puzzle section also demonstrates this issue for practice.

> falling-water


The strategy of "decomposition"

Program Made of Functions

Big picture view of a program — a program made up of functions

alt: program is made of functions, each with def

Decomposition Strategy - Divide and Conquer

"Divide and Conquer" - a classic strategy, works very well with computer code

Web Browser - 21 Million Lines Of Code

Decompose Browser into Functions


Call a Function in Python - 2 Ways

To "call" a function means to go run its code, and there are two ways it is done in Python. Which way a function is called is set by its author when the function is defined.

1. Call by noun.verb

For "object oriented" code, which is how bit is built, the function call is the noun.verb form, e.g. bit.left(). Here "left" is the name of the function. Your code calls bit functions with this form now, and in future weeks we'll use many functions with the same noun.verb syntax...

bit.left()        # turn bit

lst.append(123)   # append to list

def - Function Name and Code

Look at a def again to see what it does. Here's what def for a bit problem might look like...

def go_west(bit):
    bit.left()
    bit.paint('blue')
    ...

The def establishes that this function name, go_west, refers to these indented lines of code. the def does not run the code. It establishes that this name refers to this code. If another part of the program wants to refer to this function, it uses the name go_west.

2. Call Function By Name

The second type of function call in Python is deceptively simple. You just type the function's name with parenthesis after it. Here is what a line of code calling the above go_west function looks like:

    ...
    go_west(bit)
    ...

The word "bit" goes in the parenthesis for now. That's a parameter that we will explain in detail later.

Function Call Picture

Calling a function prompts the computer to go run the code in that function, and then comes back to where it was. Say for example the computer is running in a "caller" function, and within there is a call to a foo() function - the computer goes to run the foo() code, then returns and continues in the caller function where it left off.

The computer is only running one function at a time.


Decomposition Steps + Art

With decomposition, we want to divide the program into more manageable sub-parts.

1. Identify smaller sub-problems of the main task. Write "helper" functions that solve each sub-problem. Ideally, test and debug the helper functions before proceeding.

2. Proceed to solve the larger task, but crucially, call the helper function to solve those sub-problems. In this way, we avoid solving the whole problem as one giant piece of code.

Possibly surprising fact — Working on a series of smaller functions takes less time than writing an equivalent giant function that does the whole problem.

Here is a foreshadowing picture of the Decomposition strategy. It's funny that this picture is so abstract. And yet, at the end of today's lecture, you may come to agree that this drawing is exactly what we did.
alt: decomposition series, ending with call-the-helper

It's a little magic — call the helper, and, poof! The problem's gone.

Decomposition Example 1 - Fill Example

This example demonstrates bit code combined with divide-and-conquer decomposition. We'll write a helper function to solve a sub-part of the problem.

> Fill Example

The whole program does this: bit starts at the upper left facing down. We want to fill the whole world with blue, like this

Program Before:
alt: world without blue, bit at upper left

Program After:
alt: world filled blue, bit at lower left

Step 1. - Helper Function fill_row_blue()

First we'll decompose out a fill_row_blue() function that just does 1 row.

This is a "helper" function - solves a smaller sub-problem.

fill_row_blue() Before (pre)
bit at left facing south

fill_row_blue After (post):
row filled with blue, bit back at start position

We could have you write the code for this one, but we're providing it today to get to the next part.

Run the fill_row_blue() helper a few times (Case-1) to see what it does.

Function Pre/Post Conditions

Pre/Post - Why Do I Care?

fill_row_blue() Pre/Post

Now comes the magic step for today - calling the helper function.

Challenge Step-2: Write fill_world_blue()

Aside: Milestone Strategy

To build a big program, don't write the whole thing and then try running it. Have a partially functional "milestone", get that working and debugged. Then work on a next milestone, and eventually the whole thing is done. This is a time-saving practice. Our assignment handouts will build up each project in terms of milestones, to help build up this habit.

2 Row Milestone

As a first step, write code to just solve just the first 2 rows, without a loop. This is a test "milestone", getting code to work for an interesting sub-problem, but not the whole thing.

1. How can I get the 1st row filled?

This can be done with 1 line of code. Think function-call.

A: Call the helper function - key example for this lecture

Code this up, click Run to see what it does, though it does not solve the whole problem.

2. Where is Bit Now?

Where is bit after the call? Look at the post condition.

3. How can I get the 2nd line filled?

Move bit down to row 2. Call helper again. Use the pre/post to work with the helper.

4. Loop To Solve All Rows But The Top

Put in a while loop to move bit down the left edge until hitting the bottom. Call the helper for each moved-to row. This solves all the rows except the top row.

...
while bit.front_clear():
    bit.move()
    fill_row_blue(bit)

5. Solve The Whole Thing

Put in one call to the helper to solve the top row, then the loop solves all the lower rows.

fill_world_blue() Solution

def fill_world_blue(filename):
    bit = Bit(filename)  # provided
    fill_row_blue(bit)
    while bit.front_clear():
        bit.move()
        fill_row_blue(bit)

Decomposition - Helper Functions - Summary


Decomposition Example - Fancy Paint

> Fancy

Bit is moving past some lone blocks. We want each block to get a fancy paint job like this:

Fancy before and after
alt: fancy before
alt: fancy after

1. Helper paint_one(bit)

Helper function, paint around one block.

Pre: facing block

Post: painting done, on starter square, facing away from block

Fancy before and after
alt: fancy one before
alt: fancy one after

Helper Function paint_one(bit) Code

This code would be easy enough to write, but we're providing it in the starter code to focus on the decomposition step.

Notice also the tripe-quote """Pydoc""" at start of the function. This is a Python convention to document the pre/post of a function.

def paint_one(bit):
    """
    Begin facing square. Fancy paint
    around the square. End on start square,
    facing away from square.
    """
    bit.left()
    bit.move()
    bit.right()
    bit.move()
    bit.paint('red')
    bit.move()
    bit.right()
    bit.move()
    bit.paint('green')
    bit.move()
    bit.right()
    bit.move()
    bit.paint('blue')
    bit.move()
    bit.right()
    bit.move()
    bit.left()

2. Start code of paint_all()

First step, write the standard while-loop to move bit forward to the side of the world.

    while bit.front_clear():
        bit.move()

3. How to detect a block to draw around?

Write an if-test in the loop to detect the block. What test is True if a block is above bit? Make a little drawing to work it out.

alt: test for block

A: A good start is bit.left_clear() - however, it has exactly the opposite T/F of what we want. Use "not" to invert it to make the T/F we want. Write the if-test, then worry about the code to paint the block.

4. How to fancy paint the block?

Q: How to fancy paint the block? i.e. the code that goes inside the if.

A: Call the helper function - today's theme. Is bit facing the correct direction for the call? No, need to turn left to face the block before calling, matching the pre of the function. (Work a little drawing for these details).

5. What to do after the call?

Q: What way is bit facing after calling the helper?

A: Down - the post of the paint_one() tells us this. We cannot resume the while-loop with bit facing down (you could run it and see). The while loop had bit facing the right side of the world - turn bit to face that way. With bit's direction matching what the while-loop had before, the while loop will resume properly. (Work a little drawing for these details.)

    ...
    while bit.front_clear():
        bit.move()
        if not bit.left_clear():
            bit.left()
            paint_one(bit)
            bit.left()

Run and see

Put in the call to the helper - minding the pre and post adjustments before and after the call. Run it to see how it works.

6. There's More - 2nd Row of blocks

Look at Case-3 - another row of blocks below. Actually we want bit to fancy paint blocks both above and below its horizontal track. This will not be much work, since we have the helper function.

alt: fancy input with above and below blocks

7. How to Detect Below?

Q: What is the if-test that is True for a block below as bit goes to the right.

A: Similar to previous test, just swapping left/right: if not bit.right_clear():. Add the if-statement for the lower block below the code for the top block.

8. How to paint the lower block?

Q: How to paint the lower block?

A: Call the helper again. Need to account for pre/post as before. It's fine to use copy/paste to re-use the code for the top block, but remember to then update the details in the pasted version — it's a common mistake to forget to update a word after a paste. The helper will work, requiring only that we face the block before calling it (the helper is direction-independent in this way).

paint_all() Solution

Here's the working code. The paint_one(bit) lines are the key lines for understanding decomposition.

def paint_all(filename):
    """
    Move bit forward until blocked.
    For every moved-to square,
    Fancy paint blocks which appear
    to the left or right.
    """
    bit = Bit(filename)
    while bit.front_clear():
        bit.move()
        # Detect block above
        if not bit.left_clear():
            bit.left()
            paint_one(bit)  # Call the helper
            bit.left()
        # Block below
        if not bit.right_clear():
            bit.right()
            paint_one(bit)
            bit.right()

Fancy Paint Observations

Important CS strategies to observe in the Fancy Paint example:

Decompose out a helper for a subproblem. It's big help later on the main problem.

Need to think about the pre/post before and after the call to the helper to mesh things together.

This problem is complex enough where the make-a-drawing technique is helpful to tame the details, meshing the parts together.

Q: look at the main output - how many bit.paint('red') lines are in this program?

A: Just one. Decomposition is about making a helper and then using it heavily, in effect making one copy of the code and then re-using it everywhere it applies. This is how there's only a single bit.paint('red') and yet there's so much red in the output.


Some other points to clean up, if we have time.

Helpers At Top - Convention

There is a convention to put the helper functions first in the program text. The larger functions that call them down below. This is just a habit; Python code will work with the functions in any order. Placing the helpers first does have a kind of logic — the paint_one() helper is first, and it is the simplest and does not depend on another function. Then the function that uses it is below.

paint_one() Helper Pydoc Triple Quote

At the top of each function is a description of what the function does within triple-quote marks. This is a Python convention known as "Pydoc" for each function. The description is essentially a summary of the pre/post in words, see the """ section in here:

def paint_one(bit):
    """
    Begin facing square. Fancy paint
    around the square. End on start square,
    facing away from square.
    """
    ...

For now, the starter includes the written Pydoc, but later we'll have an exercise where the problem statement directs the student to write the Pydoc.

(optional) Extra Decomposition Example - Cover

> Cover Example

Bit starts next to a block of solid squares (these are not "clear" for moving). Bit goes around the 4 sides clockwise, painting everything green.

cover_square() Before (pre):
cover at start

cover_square() After (post):
cover after all 4 sides down

Cover Helper - cover_side()

Code for this is provided.

cover_side() Before: on top of first square, facing direction to go
alt: cover_side before

cover_side() After - move until clear to the right, painting every square green
alt: after one cover_side

1. Run cover_side()

cover_side(bit) specification: Move bit forward until the right side is clear. Color every square green.

Run this code with case-1, to see what it does. (code provided)

2. Challenge: write code for cover_square()

cover_square(bit) specification: Bit begins atop the upper left corner, facing right. Paint all 4 sides green. End one square to the left of the original position, facing up.

Add code to paint the top and right sides. Key ideas: