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

Back to the problem set 7 page

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): 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: 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: 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:

• `trunkLength`: This is the length of the line drawn to get to the base of the three branches. The left and right branches for a given tree have `trunkLength` values equal to 60% of the base value, while the middle branch has `trunkLength` equal to 75% of the base value.
• `leafLength`: This sets a lower limit on the trunk length. If the `trunkLength` argument is less than this value, a leaf will be drawn (see below) instead of a branching tree.
• `branchAngle`: This controls the angle between the middle branch and the two side branches. The branch angle for all three branches at each step is 80% of the base branch angle, so smaller sub-branches have more acute angles than the whole tree.
• `branchColor`: This determines the color of each branch, including the trunk.
• `leafColor`: This is used as the color for the leaves.

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: 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')``  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.