BYU logo Computer Science

To start this assignment, download this zip file.

The following guide pages cover material needed for this assignment:

Lab 2c โ€” Decomposition

Preparation

Reminder, you should work in teams of 2 or 3 when solving the lab problems. Learning to code together is an important part of this class.

1 minute

Download the zip file for this lab, located above. This zip file has worlds that you will use for Bit. Extract the files and put them in your cs110 directory in a folder called lab2c.

Leap

5 minutes

Bit starts in this world:

an empty 6x3 world

and runs this code:

from byubit import Bit


def leap(bit):
    if bit.front_clear():
        bit.move()
    if bit.front_clear():
        bit.move()
    if bit.front_clear():
        bit.move()


def paint_spots(bit):
    while bit.front_clear():
        bit.paint('blue')
        leap(bit)
    bit.paint('blue')


def fill_green(bit):
    while bit.front_clear():
        bit.paint('green')
        bit.move()
    bit.paint('green')


@Bit.empty_world(6, 3)
def go(bit):
    fill_green(bit)
    bit.left()
    bit.left()
    paint_spots(bit)


if __name__ == '__main__':
    go(Bit.new_bit)

Answer these questions to make sure you understand the code.

  1. What does leap do?
  • How would the result change if the last two ifs were changed to elif?
  1. What does paint_spots do?
  2. What does fill_green do?
  3. What will the final world look like?

Copy and paste this code into a new file called leap.py, and run it to be sure you have the right answer.

Discuss with the TA if there is anything you donโ€™t understand about functions.

Surround

15 minutes

Bit starts in a world that has some rectangular blocks. Here is world #1

a world with some black rectangles

and world #2:

a world with some black rectangles

Bit needs to surround each of the blocks with green squares:

the sides of the black rectangles have green squares

the sides of the black rectangles have green squares

  1. This is a decomposition problem! Work with a friend to draw out your solution. See the guide on decomposition for examples of how to do this.

  2. After you draw it out, discuss the overall algorithm, in plain English, with the TA.

  3. Then come up with function names that could be used to solve the problem.

  • Tip: Surrounding a block can be done with a single while loop, using the event stream pattern.
  • When should bit stop?
  • What should be the while condition?
  • What is the event that happens along the way?
  1. Once you have a good idea of how to solve it, write code, using functions.

You can find starter code in surround.py.

Blossoms

15 minutes

Bit starts in a world that has some empty pools. Here is world #1

a world with empty pools

and world #2:

a world with some empty pools

Bit needs to fill each pool with water and then plant a flower on the right side:

the pools filled, with a flower on the right edge

the pools filled, with a flower on the right edge

  1. Guess what, this is another decomposition problem! :-) Work with a friend to draw out your solution.

  2. After you draw it out, discuss the steps with the TA.

  3. Be careful about filling the pool. A neat way to do it involves moving bit from left to right across the top, from one cliff edge to the other. Along the way, paint down and up.

  4. Once you have a good idea of how to solve the problem, write code, using functions.

You can find starter code in blossoms.py.

Preview Homework

10 minutes

Discuss the next set of homework problems. Are there any difficulties you anticipate? Talk about decomposition strategies, but each student should write their own code.

Grading

To finish this lab and receive a grade, take the canvas quiz.

We are providing a solution so you can check your work. Please look at this after you complete the assignment. ๐Ÿ˜Š