Problem Set 3 - Due Tue, Feb 20 at 23:59

Reading

  1. Slides and notebooks from Lec 04 Functions. You do not need (in fact, should not use material from) Lec 05 Divide/Conquer/Glue with Pictures, Lec 06: Booleans, Logical Expressions, and Predicates and Lec 07: Conditionals.
  2. Problems and solutions from Lab 03 Functions
  3. Think Python, Chapter 3: Functions

About this Problem Set

This problem set will give you practice with abstraction, problem-solving by recognizing patterns, functions, and the Divide, Conquer, and Glue (DCG) problem-solving strategy.

  1. In both tasks you will create your own functions and practice breaking down a problem into simpler parts.
  2. In Task 1 (individual task), you will use functions to display a diamond pattern made out of text lines generated from a small set of strings.
  3. In Task 2 (partner-optional task), you will create butterfly patterns using functions. In this problem, having a partner is optional, but strongly recommended. If you want to have a partner, use this shared Google Doc. Remember, you can talk with other individuals and teams about high-level problem-solving strategies, but you cannot share any code with them.
  4. The CS111 Problem Set Guide gives an overview of psets, including a detailed description of individual and partner tasks.
  5. Follow the style guidelines in the CS 111 Code Style Guide

All code for this assignment is available in the ps03 folder in the cs111/download directory within your cs server account.


Task 1: Diamond Pattern

This is an individual problem which you must complete on your own, though you may ask for help from the CS111 staff.

In the provided diamonds.py, define a zero-parameter function named diamondPattern. When diamondPattern is invoked on zero arguments in the interactive Python pane, it should display the pattern asterisks shown below:

In [17]: diamondPattern()
  *         *         *         *
 * *       * *       * *       * *
* * *     * * *     * * *     * * *
 * *       * *       * *       * *
  *         *         *         *
       *         *         *         *
      * *       * *       * *       * *
     * * *     * * *     * * *     * * *
      * *       * *       * *       * *
       *         *         *         *
  *         *         *         *
 * *       * *       * *       * *
* * *     * * *     * * *     * * *
 * *       * *       * *       * *
  *         *         *         *
       *         *         *         *
      * *       * *       * *       * *
     * * *     * * *     * * *     * * *
      * *       * *       * *       * *
       *         *         *         *
  *         *         *         *
 * *       * *       * *       * *
* * *     * * *     * * *     * * *
 * *       * *       * *       * *
  *         *         *         *
       *         *         *         *
      * *       * *       * *       * *
     * * *     * * *     * * *     * * *
      * *       * *       * *       * *
       *         *         *         *
  *         *         *         *
 * *       * *       * *       * *
* * *     * * *     * * *     * * *
 * *       * *       * *       * *
  *         *         *         *
       *         *         *         *
      * *       * *       * *       * *
     * * *     * * *     * * *     * * *
      * *       * *       * *       * *
       *         *         *         *

In Canopy, open diamonds.py. Notice that it contains the following variable definitions.

# pre-defined strings
zeroStar  = '     '
oneStar   = '  *  '
twoStar   = ' * * '
threeStar = '* * *'
empty     = ''

Notes


At the very end, once you're satisifed with your diamondPattern function, you should check your program with Codder. Codder will not only verify that the diamond patter is correct, but it will also check that you have satisfied various restrictions specified above for your code.


In this problem, having a partner is optional, but is strongly recommended. If you want to have a partner, use this shared Google Doc.

Task 2: Butterflies

In this task, your ultimate goal is to create a canvas populated with butterflies that looks like this:

You will accomplish this task by completing several subtasks. You should do these subtasks in order.

Subtask a: makeWing

In this subtask, you must define a function named makeWing that takes two parameters --- first a wing color and then a spot color --- and returns a Layer like the one shown in the following call:

makeWing('pink', 'yellow')

The wing is always an ellipse (in this case, pink) and the spot is always a circle (in this case, yellow). The dimensions and positioning of the parts of the returned wing layer are always the same, and are specified in the following picture:

makeWing('pink', 'yellow')

Note that the layer includes a small cyan dot at the left edge of the ellipse that explicitly marks the (0,0) reference point of the returned layer. This dot is part of the returned layer. It is always cyan, and always has radius 3.

You should add your makeWing function to the part of butterflies.py where it says:

#******************************************************************************                                      
# Put your makeWing function definition here                                                                         
#******************************************************************************     

Notes:

Subtask b: makeWingPair

In this subtask, you must define a function named makeWingPair that takes two parameters --- first a wing color and then a spot color --- and returns a Layer like the one show in the following call:

makeWingPair('pink', 'yellow')

A wing pair is always a pair of wings with the same colors containing a full-sized wing rotated counterclockwise by 60 degrees and a 75%-sized wing rotated clockwise by 30 degrees. As indicated in the following picture, the cyan dots of the two wings should coincide at the point (25,0) in the returned layer.

makeWingPair('pink', 'yellow')

Note that the layer returned by makeWingPair includes a small yellow dot that explicitly marks the (0,0) point of the returned layer. This dot is part of the returned layer. It is always yellow, and always has radius 3.

You should add your makeWingPair function to the appropriate part of butterflies.py, as indicated by a comment.

Notes:

Subtask c: makeButterfly

In this subtask, you must define a function named makeButterfly that takes six parameters and returns a Layer like the one shown in the following call:

makeButterfly('purple', 'orange', 400, 500, 1.0, 0.0)

The six parameters, in order, are:

  1. The color of the butterfly body and spot on the wing (these are always the same color)
  2. The color of the wing
  3. The x coordinate of the butterfly layer's reference point, which coincides with the yellow reference dot in the butterfly's body.
  4. The y coordinate of the butterfly layer's reference point.
  5. The factor by which a full-sized butterfly is scaled about its yellow dot reference point
  6. The angle by with the butterfly is rotated about its yellow dot reference point

A full-sized butterfly consists of two full-sized wing pairs whose yellow dot reference points coincide at within the rectangular body (see size details below). This yellow dot is also the (x,y) reference point of the returned layer.

makeButterfly('purple', 'orange')

You should add your makeButterfly function to the appropriate part of butterflies.py, as indicated by a comment.

Notes:

Subtask d: butterflySky

Your final subtask is to define a zero-parameter function butterflySky that creates the canvas with 5 butterflies shown at the beginning of this task. Here is the same picture with a grid and reference points turned on:

You should be able to tell most canvas and butterfly parameters from the above picture. Here are a few details that may not be apparent:

Notes:

Task 3: Honor Code File

Yur honor code submission for this pset will involve defining entering values for the variables in the honorcode.py file.

Remember to run otterInspect.py one final time before you submit to check that all your diamond functions work, and that your honor code form is complete.


How to turn in this Problem Set

Soft-copy submission

Hard-copy submission

There is no hard copy submission. Problem set submission is entirely electronic.