Problem Set 7 - Due Fri Dec 11 at 23:59

Back to the problem set 7 page

Task 1: Genetic Sequences

The task 1 rubric shows how this task will be graded, and can be used as a checklist to make sure you are done with the task.

You have the option to work with a partner on this task if you wish. Working with a partner requires more work to coordinate schedules, but if you work together and make sure that you are both understanding the code you write, you will make progress faster and learn more. This Piazza post can be used to find a partner for this task.

Put all of your work for this task into the file genetics.py.

For part A of this task, you will create a non-fruitful recursive tree function that draws a branching pattern using turtle graphics, quite similar to the tree drawn in problem set 3 task 1. Here is an example of the output (when using the provided testing code):

A branching tree with a brown trunk and branches and orange leaves. The overall shape is similar to that of a maple leaf, with three prominent segments of leaves.

After completing part A, in part B you will create a fruitful version called autumnTree which reports the number of leaves on the tree, and which is also randomly missing some leaves.

Part A: tree

In the tree, everything starts with a trunk, and the first parameter of the tree function specifies the length of the trunk. Unless it is just a leaf, each tree has three branches: one to the left, one to the right, and one in the middle. Of course, each of these branches is actually an entire smaller tree, as shown in this image:

Three trees of different sizes with indicators showing how the larger trees contain individual branches that are exactly the same as the entire smaller trees.

Note that the three trees in this image have been drawn starting from different positions and orientations just to show how they fit into each other. Here is another image that shows how different trunkLength parameters result in different trees if the other parameters are held constant:

Six trees of different trunk lengths (labeled 12, 16, 20, 24, 28, and 32). The first is just a thick orange line with rounded ends, the next has a brown stem plus three orange lines branching out from the tip, and the third has two stems with five leaves (two branching from the middle and three from the tip). The fourth tree (trunk length 24) now has a base stem, with to branches angled out from the middle that each have three leaves, plus three leaves at the end of the middle branch. The fifth tree (trunk length 28) has a base stem, two three-leaf branches coming out of that, and in the middle has an entire smaller version of the third tree (with 5 leaves). The final tree (length 32) has two of these 5-leaf branches coming from its middle, and the center branch has the same structure as the fourth picture (three three-leaf branches).

The tree.py file contains a blank definition of the tree function, which is missing a docstring. As usual, you must fill in the docstring for the tree function with your own description of how it works, and you should do this before you begin to write your code.

The tree function takes the following parameters:

In addition to these parameters, the pen size for each branch is 1/10th its length. For leaves, draw a straight line with length equal to the given trunkLength and pen size set to 1/2 of that trunk length. Note that the length of each leaf is not necessarily equal to leafLength, because leaves are drawn when the trunkLength parameter is less than or equal to the leafLength parameter.

Your first job is to fill in the tree function in tree.py so that it draws the exact shape shown above when called with the following arguments (which are the defaults in the testing code):

tree(100, 20, 70, 'Tan4', 'DarkOrange2')

Note that if you call that yourself without any setup, the image should look like this:

A tree pointing to the right instead of upwards and partially cut off by the edge of the window.

Note one more important fact: when the tree function is finished, the turtle cursor ends up back in the exact position it started from. This is a critical factor in making the recursion work properly (tree must be recursive and may not use loops).

Part B: autumnTree

Now that your non-fruitful tree function is working, you must create a fruitful version which reports the number of leaves on the tree. To make this task interesting, in this autumnTree function, whenever a leaf would be drawn, there's a 50% chance that nothing is drawn instead (you must use the provided shouldDrawLeaf function to determine this, and you may not use the random module besides via that function).

Here are two examples of autumn trees drawn using the following function call:

autumnTree(100, 20, 70, 'Tan4', 'DarkOrange2')
A tree just like the other full trees above, except that some of the leaves are missing at random. Another tree randomly missing some leaves, this time with a different set of leaves missing.

Because of randomness, these two trees are missing different sets of leaves; these function calls returned the values 74 and 80 respectively. For testing purposes, you may want to use a smaller trunk length (e.g., 36) so that you can visually check whether your function correctly returns the total number of leaves that it drew.

Except for the fact that some leaves are skipped, the structure drawn by autumnTree is exactly the same as that drawn by the tree function. The tricky part of this task is to figure out how to make your function fruitful so that it correctly returns the number of leaves drawn in all cases.


When you are done with this task, you should submit it via the Ocean server. If you worked with a partner, both you and your partner must submit a copy of the file that you produced. Remember to follow the submission instructions when you are done with the whole problem set.