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:
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.
- What does
leap
do?
- How would the result change if the last two ifs were changed to elif?
- What does
paint_spots
do? - What does
fill_green
do? - 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
and world #2:
Bit needs to surround each of the blocks with green squares:
-
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.
-
After you draw it out, discuss the overall algorithm, in plain English, with the TA.
-
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?
- 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
and world #2:
Bit needs to fill each pool with water and then plant a flower on the right side:
-
Guess what, this is another decomposition problem! :-) Work with a friend to draw out your solution.
-
After you draw it out, discuss the steps with the TA.
-
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.
-
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. ๐