BYU logo Computer Science

To start this assignment, download this zip file.

Homework 1b — Functions

1. One Tree

You are given some code in one_tree.py. Bit starts in a 5x4 world:

blank world

Bit should draw a tree:

one tree

In the code we have given you, we have decomposed the problem for you:

from byubit import Bit


def move_to_tree(bit):
    """ Moves to the trunk """
    pass


def draw_trunk(bit):
    """ Draws the trunk (two red squares)  """
    pass


def draw_branches(bit):
    """ Draws the branches """
    pass


def go_back_down(bit):
    """ Moves back down to the ground, below the right-most branch, facing right. """
    pass


@Bit.worlds("one_tree")
def one_tree(bit):
    move_to_tree(bit)
    draw_trunk(bit)
    draw_branches(bit)
    go_back_down(bit)


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

We want you to see an example of how each function does one small thing.

You should be able to draw the tree by filling in each of these four functions, without using any glue code.

2. Trees

You are given some code in trees.py. Bit starts in a 13x4 world:

blank world

Bit needs to draw three trees, with some space between them:

three trees next to each other, with some space between

Copy and paste your one_tree() function and the other helper functions from the previous problem. Be sure to remove the @Bit.worlds("one_tree") decorator from the one_tree() function.

This problem shows you how using functions makes it easy to re-use code. In future units, you can use an import statement to reuse functions, but for the Bit units you will need to copy and paste.

3. Boxes

You are given some code in boxes.py. Bit starts in a 3x10 world:

blank world

Bit needs to stack three boxes, each sitting on a pallet:

three boxes stacked on top of each other, each on a pallet

The boxes are blue and the pallets are red.

This problem is meant to give you practice with decomposition, so you should write some additional functions and then call them from the stack_boxes() function.

4. Quilt

You are given some code in quilt.py. Bit starts in a 13x3 world:

blank world

Bit needs to draw a quilt pattern:

quilt pattern

This problem is meant to give you practice with decomposition, so you should write some additional functions and then call them from the make_a_quilt() function.

Grading

Turn your Python files in on Canvas, which will link you to Gradescope.

ActivityPoints
one_tree.py5
trees.py5
boxes.py5
quilt.py5

Manual grading will focus on the appropriate level of decomposition by writing functions.