Computer Science

# Lab 3 — Decomposition with Bit

To begin, download lab-3-files.zip. This is a zip file, so you will need to extract its contents. You should put the files inside of a folder called lab-3 in your CS 110 folder. These files contain worlds. A world is a file that contains a description of what Bit sees.

You should have a folder called lab-3/worlds/, and then a bunch of files in there.

You are going to write your code in the lab-3/ folder.

## Grass and Sky

Remember the Grass and Sky problem? Start with:

from byubit import Bit
bit = Bit.new_world(5,3)

Then make the world so it looks like this:

Use functions for this problem!

• Write a function that can color a row. It should take a color as an argument, so it can paint whatever color you give the function.
• Write a function that can move back to the beginning of a row, assuming bit is at the end of a row to start

Using these functions, solve the grass and sky problem.

## Smile!

Help Bit draw blue smiles at each green square. Each green space corresponds to the left eye of the smile.

The starting world is in smiles-start.txt. It looks like this:

You can write your code in smiles.py and use this to load the starting world:

from byubit import Bit

The ending world should look like this:

### Use functions!!

What are the different abstractions that will help you solve this problem? Write a function for each. Think carefully about pre and post conditions of each function. You can do it!

Your solution should have one or more helper functions.

### Testing

If your function to draw the smiles is draw_smiles(), you can test your solution with the following code:

## Fix the Forest

Fix all the trees!

• How can you decompose this problem?
• For each function you define, what are the pre and post conditions?
• How do the functions fit together?

The starting world looks like this:

The ending world looks like this:

You can test your code with:

## The Desert Shall Blossom as a Rose

This problem will especially help you get ready for the project.

Fill the holes with water, and plant flowers near the water.

The starting world looks like this:

The ending world looks like this:

You can test your code with:

### Tips

Remember that decomposition is the key to solving complex problems. You may want to start by filling in all the puddles. This will itself take a few different functions, maybe one to move to the edge of a puddle, another to fill in one column of a puddle, and so forth.

You could then see if you can fill in puddles without drawing any flowers:

Now add functions for growing the flowers!

## Lessons

What we want you to get from this lab:

• You feel confident writing functions

• You can take a complex problem and decompose it into a set of functions, each of which accomplish a small piece of the problem

• You think carefully about pre-conditions and post-conditions for functions

• You can clearly document your functions with docstrings

• You can figure out what went wrong when something unexpected happens

• Hopefully you had fun!