Problem Set 3 - Due Tue, Sep 25 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 or 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.

Other notes:

 


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.


Task 2: Butterflies

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

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

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

If you wrote any function invocations or print statements in your Python files to test your code, please remove them, comment them out before you submit, or wrap them in the if __name__=='__main__' block at the end of the .py file. Points will be deducted for isolated function invocations or superfluous print statements.

It's a good idea to run Codder one last time on diamonds.py and butterflies.py before you submit your ps03 folder.


How to turn in this Problem Set

Softcopy (electronic) submission

Hardcopy (paper) submission

There is no hardcopy submission. Problem set submission is entirely electronic.