BYU logo Computer Science

To start this assignment, download this zip file.

The following guide pages cover material needed for this assignment:

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

Now that you have learned about functions, we will be grading you on function names and appropriate decomposition.

We recommend breaking problems into the biggest chucks first. For example, the biggest repeated section in trees.py is painting a full tree. You should have a function that paints an entire tree. Then, you can break that function into smaller parts by creating more functions with one clear purpose.

There are multiple ways to do these problems well, but there are a few things that indicate that your code could be better. One is long sections of duplicate code. Duplicate code should be put in a function, and you should call the function multiple times.

The other indicator is very long functions. Even when you don’t have repeated code, breaking a problem into parts can help your code be more readable. one_tree.py is an excellent example of decomposing a problem even when sections of code are not repeated.

Refer to the Quality Code guide page for detailed explanations and examples on each of these rubric criteria.

RubricPoints
Whitespace1
Naming1
Decomposition4
Intent4
Total10