Today: if statement, ==, function call, decomposition

Reminders

Lego Bricks!

Aside: Bit Controls


If Statement - Logic

While loop is power. If-statement is control, controlling if lines are run or not

If-Demo

We'll run this simple bit of code first, so you can see what it does. Then we'll look at the bits of code that go into it.

Problem statement: Move bit forward until blocked. For each moved-to square, if the square is blue, change it to green. (Demo: this code is complete.).

> Change-Blue Demo

For reference here is the code:

def change_blue(filename):
    bit = Bit(filename)
    while bit.front_clear():
        bit.move()
        if bit.get_color() == 'blue':
            bit.paint('green')

If Statement Syntax

Syntax 4 parts (similar to "while"): if, boolean test-expression, colon, indented body lines

if test-expression:
    body lines

If Statement Operation

Look At Test Expression

Here are the key lines of code:

    ....
    if bit.get_color() == 'blue':
        bit.paint('green')
    ....

1. bit.get_color() Expression

Expression Visualization

Suppose bit is on a squared painted 'blue', as shown below. Here is diagram - visualizing that bit.get_color() is called and it's like there's an arrow coming out of it with the returned 'blue' to be used by the calling code.

alt: get_color() returns the value 'blue'

2. == Compares Two Values - Boolean

Now Look at Change-Blue Again

Look at the if-statement. What is the translation of that into English? ..

For each moved-to square. If the square is blue, paint it green.

while bit.front_clear():
    bit.move()
    if bit.get_color() == 'blue':
        bit.paint('green')

Other Examples == !=

if bit.get_color() == 'red':
    # run this if color is 'red'

if bit.get_color() == None:
    # run this if square is not painted

if bit.get_color() != 'red':
    # run this if square is anything but 'red'
    # e.g. 'green' 'blue' or None

The Fix Tree Problem

A medium hard problem to look at the thinking and coding process

> Fix Tree

Before:
alt: fix-tree before

After:
alt: fix-tree after

Important Ideas - Thinking Drawing and Coding

We have art for these. No expense has been spared!

1. Don't do it in your head


alt: not in your head

Don't do it just in your head - too much detail. Need to be able to work gradually and carefully.

2. Make a Drawing

Draw a typical "before" state.
alt: before state

3. What is a next-step goal - what would that look like?


alt: draw a next-step goal

Look at the before state - what code can advance this?

4. Question: what code?

You have the current state. What code could advance things to the next goal?

Q: Look at those bit positions. When do you want bit to move, and when not to move?

A: Want bit to move when the square is not red. What is the code for that?

Aside: if you were talking to a human, you would just say "make it look like this" and point to the goal. But not with a computer! You need to spell out the concrete steps to make the goal for the computer.

In this, case we're thinking while-loop. Draw in the various spots bit will be in. What is a square where we do not want bit to move? When bit is on the red square. Otherwise bit should move. What does that look like in code?
alt: code to next state

Code looks like...

while bit.get_color() != 'red':
    bit.move()

5. Try The Code, See What It Does

Ok, run it and see. Sometimes it will not do what we thought.

6. Ok what is the next goal.

Ok what is the next goal? What would be code to advance to that?
alt: next goal, get to tree

I think a reasonable guess here is similar to the first loop, but painting red, like this

while bit.get_color() != 'green':
    bit.move()
    bit.paint('red')

7. OOPS Not What Expected!

OOPS, that code looks reasonable, but does not do what we wanted. The question is: what sequence of actions did the code take to get this output? That's a better question than "why doesn't this work".

Go back to our drawing. Think about the paint/red and the while loop, how they interact.
alt: look at to-tree structure, why it does not work with that code

8. Solution

The problem is that we paint the square red, and then go back to the loop test that is looking for green, which we just obliterated with red. One solution: add an if-statement, only paint blank squares red, otherwise leave the square alone.

That gives us this solution, which works perfectly..

def fix_tree(filename):
    bit = Bit(filename)
    while bit.get_color() != 'red':
        bit.move()
    bit.left()
    while bit.get_color() != 'green':
        bit.move()
        if bit.get_color() == None:
            bit.paint('red')

9. Thinking - Drawing - Coding

Use a drawing to think through the details of what the code is doing. It's tempting to just stare at the code and hit the Run button a lot! That doesn't work! Why is the code doing that?

Or put another way, in office hours, a very common question from the TA would be: can you make a little drawing of the input here, and then look at your line 6, and think about what it's going to do. All the TA does is prompt you to make a drawing and then use that to think about your code.


Decomposition - Program and Functions

"Divide and Conquer" - a classic strategy, works great with code

Functions - The Wizard of EarthSea

In the Wizard of EarthSea novels by Ursula Le Guin .. each thing in the world has its secret, true name. A magician calls a thing's true name, invoking that thing's power. Function calls work like this - a function has a name, you call a function by its name, invoking its power.

How To Call a Function

We've seen def many times - defines a function. Note that each function has a name and body lines of code, like this "go_west" function:

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

1. Call by noun.verb

To "call" a function means to go run its code, and there are two common ways it is done in Python. One way to call a function in Python is the "object oriented" form, aka noun.verb. The bit functions use this method, e.g. bit.left(), where "left" is the name of the function.

2. Call by name

The other way to call a function, which we will use here, is simply writing its name with parenthesis after it. For the above function named "go_west", code to call it looks like:

    ...
    go_west(bit)
    ...

Both of these function-call styles are widely used in Python code.

What Does Function Call Do?

Say the program is running through some lines, which we'll call the "caller" lines. Then the run gets to the following function call line; what happens?

    # caller lines
    bit.right()
    go_west(bit)  # what does this do?
    bit.left()
    ...

What the go_west(bit) directs the computer to do: (a) go run the lines inside the "go_west" function, suspend running here in the caller. (b) When go_west() is done, return to this exact spot in the caller lines and continue running with the next line. In other words, the program runs one function at a time, and function-call jumps over to run that function.


Fill World Blue Example

This example demonstrates bit code combined with divide-and-conquer decomposition.

> 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

1. - Decompose fill_row_blue() "helper" function

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

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

Run the fill_row_blue a few times, see what it does.

Function Pre/Post Conditions

Pre/Post - Why Do I Care?

Look at fill_row_blue() Function

def fill_row_blue(bit):
    bit.left()
    bit.paint('blue')
    while bit.front_clear():
        bit.move()
        bit.paint('blue')
    bit.right()
    bit.right()
    while bit.front_clear():
        bit.move()
    bit.left()
-Now comes the key, magic step for today. First, know what fill_row_blue() does exactly, pre/post.

2. Write fill_world_blue()

fill_world_blue() Functions - A Great Deal!


More Bit Tests: bit.left_clear() + not

if bit.left_clear():
    # run here if left is clear

if not bit.left_clear():
    # run here if left is blocked, aka not-clear

Decomposition Example / Exercise: 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 Before (pre):
world without blue cover done

cover_row() After (post):
alt: after one cover_side

cover_square() After (post):
world filled blue done

1. Run cover_side()

cover_side(bit): Move bit forward until the right side is clear. Color every square green. (demonstrates not left-clear test).

Run this code with case-1, see what it does. Study the code to see how it works, think about its pre/post (make a little drawing).

2. Work on cover_square()

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

Start by calling cover_side() once. How can we solve the whole thing? How to address the pre/post conditions of cover_side()? Key ideas: