Problem Set 2 - Due Tue, Sep 19 at 23:59

Reading and helpful Information

  1. The CS111 Problem Set Guide gives an overview of psets, including a detailed description of individual and partner tasks
  2. Goldwasser & Letscher, Chapter 3, sections 3.3-3.6, pages 103-110.
  3. Slides and notebooks from Lec 2 and Lec 3.
  4. Problems and solutions from Lab 2.
  5. Our CS 111 Code Style Guide

About this Problem Set

  1. Students with last names from M to Z are expected to make use of the CS 111 Google Group this week to either ask a question or answer one.

  2. Task 1 is an individual problem in which you will get practice with debugging in the context of fixing two misbehaving programs.

  3. Task 2 is a task that explores abstraction in the context of cs1graphics transformations and layers. In this problem, having a partner is optional. If you want to have a partner, use this shared Google Doc to find a pair programming partner and record who your pair partner is. Please do not use cs111-fall17 to find partners.

  4. Collaboration and honor code: you can talk with other individuals and teams about high-level problem-solving strategies, but you cannot share any code with them.

  5. The CS111 Problem Set Guide gives an overview of psets, including a detailed description of individual and partner tasks.

  6. Follow the practices discussed in our CS111 Code Style Guide.

  7. Both problems on this pset are new, so we do not have any information about the time taken on them during previous semesters.

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


Task 1: Debugging

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

Background

It is rarely the case that code works the first time you test it. It often contains one or more so-called bugs that cause it to behave incorrectly. So much of the time you spend programming in the course will involve debugging, which is the process of finding and removing bugs.

One of the themes of this course is learning strategies for debugging. So we will have some problem set problems that focus on debugging. This is the first such problem, but there will be others. In these problems you will be given code that does not work, and your goal will be to debug it until it does work.

Here's a simple example of a program that does not behave as expected:

  username = raw_input('What is your name? ') # line 1
  print 'Hello, username'                     # line 2
  n = raw_input('Enter an integer n: ')       # line 3
  print 'n is', n, '; 2*n is', 2*n            # line 4

For example:


What is your name? Wendy
Hello, username

Enter an integer n: 23
n is 23 ; 2*n is 2323

This program misbehaves in two ways:

  1. Rather than displaying the name (Wendy in this case) entered by the user, it always displays the string username.
  2. Rather than showing the double of the integer n for the expression 2*n (46 in this case) it shows the two-fold concatenation of the the digts in the number ("2323" in this case).

The first bug is due to the fact that in line 2, the occurrence of username inside the printed string denotes the literal string "username" and not the value of the variable named username. This can be fixed by changing line 2 to be

  print 'Hello, ' +  username        # fixed line 2, version 1, for 1st bug

or

  print 'Hello,', username           # fixed line 2, version 2, for 1st bug

The second bug occurs because in the expresion 2*n the value of n is the string of digits entered by the user (e.g., the 2-digit string "23") rather than the integer 23. This can be fixed by using the int() function to convert the string of digits "23" to the integer 23. One way to apply this fix is to change line 4 to be

  print 'n is', n, '; 2*n is', 2*int(n)      # fixed line 4 for 2nd bug

Alternatively, we could leave line 4 unchanged, and instead change line 3 to be

  n = int(raw_input('Enter an integer n: ')) # fixed line 3 for 2nd bug. 

With either combination of fixes, the program now works as expected:


What is your name? Wendy
Hello, Wendy

Enter an integer n: 23
n is 23 ; 2*n is 46

This example illustrates that (1) even a very short program can contain multiple bugs and (2) often there is more than one way to fix a bug.

A Note on print

Most examples involving print that you have seen in the course up to this point involve calling it with a single parenthesized argument that's a string. For example, suppose that:

Then:

print('i is ' + str(i) + '; f is ' + str(f) + '; s is ' + s)

displays the characters

i is 42; f is 6.023; s is bunny

However, it turns out that in Python 2.7 (the version we use), print is not really a function and it can be used in some ways that are both special and convenient. In particular,

For example:

print 'i is', i, '; f is', f, '; s is', s

will also displays the characters

i is 42; f is 6.023; s is bunny

Passing comma-separated values with arbitrary types to print is often more convenient that having to convert the values to strings and concatenating them into one big string.

Task 1a: Debugging Numbers

Bud Lojack is experimenting with a Python program that processes numbers entered by the user. He wants his program to behave as shown in the following examples, where text highlighed in magenta is entered by the user.

Sample Execution #1


Enter a nonnegative integer intNum: 17
The integer you entered is 17
Three times intNum is 51
17 concatenated copies of X is XXXXXXXXXXXXXXXXX
The integer remainder of 17 divided by 3 is 2
The integer quotient of 17 divided by 3 is 5
The floating point quotient of 17 divided by 3 is 5.66666666667
The number of digits in 17 is 2

Enter a floating point number floatNum: 143.816
floatNum is 143.816
143.816 rounded to two places is 143.82
143.816 truncated to an integer is 143
143.816 rounded to an integer is 144

The maximum of 17 and 143.816 is 143.816

Sample Execution #2


Enter a nonnegative integer intNum: 6
The integer you entered is 6
Three times intNum is 18
6 concatenated copies of X is XXXXXX
The integer remainder of 6 divided by 3 is 0
The integer quotient of 6 divided by 3 is 2
The floating point quotient of 6 divided by 3 is 2.0
The number of digits in 6 is 1

Enter a floating point number floatNum: 3.141
floatNum is 3.141
3.141 rounded to two places is 3.14
3.141 truncated to an integer is 3
3.141 rounded to an integer is 3

The maximum of 6 and 3.141 is 6

Click here to see Bud's program, which attempts to implement the above behavior. His progam can also be found in the ps02 folder in the file debugNums.py.

Bud's program has numerous bugs that prevent it from working properly. Your goal in this subtask is to fix Bud's program so that it behaves as indicated by the above examples. In the ps02 folder is a file debugNumsFixed.py that initially is a copy of Bud's program with some additional header information at the top that you should fill out and an extra triple-quoted comment section at the bottom (more on this below). You should modify debugNumsFixed.py by finding and fixing all the bugs it contains, so that ultimately it has the correct behavior.

Notes:

Task1b: Debugging Graphics

As part of experimenting with layers and transformations in cs1graphics, Lois Reasoner wishes to create the following picture, which contains 4 transformed versions of a face:


Relative to the upper left face:

Lois writes a program in an attempt to create the above picture. Click here for Lois's program, a copy of which can be found in the ps02 folder as debugGraphics.py. Although it is structured well and in many ways is close to correct, Lois's program generates the following incorrect picture due to numerous bugs:


Your goal in this subtask is to fix Lois's program so that it generates the correct picture. In the ps02 folder is a file debugGraphicsFixed.py that is initially a copy of Lois's buggy debugGraphics.py program with some extra header information at the top that you should fill out and an extra triple-quoted comment section at the bottom (more on this below). You should modify debugGraphicsFixed.py by finding and fixing all the bugs it contains, so that it ultimately generates the correct picture.

Notes:


Task 2: Bunny Money Pattern

This task is required but you have the option to work with a partner on it if you wish. We strongly encourage you to work in pairs on this problem. Use this shared Google Doc to find a pair programming partner and record pair programming partnership.

Your goal in this task is to generate the following picture by fleshing out the file bunnyMoneyMosaic.py in the ps02 folder:


There is a lot of stuff going on in this picture. You can see that one pattern is repeated, at different sizes and rotations. Let's call this pattern bunnyMoney, as shown below (enlarged):

The bunnyMoneyMosaic.py file begins by importing the following shapes to get you started; the rest you will create using cs1graphics.

Two graphics that are provided for you:

Description Graphic
bunny
(imported as a Layer from bunny.py)
with the reference point marked in orange
wedge
(imported as a Polygon from wedge.py)
shown with an orange fillColor

In general, we recommend that you start with pencil and paper (as opposed to jumping into Canopy). You want to build your code incrementally, making sure that smaller parts work before attempting the entire picture.

Useful bunnyMoney information:

  1. The large green (lightgreen) rectangle has a width of 600 and height of 300
  2. The blue (navy) circle in the center of the bunnyMoney has a radius of 75
  3. Each of the small wedges + bunny on the navy circle are one-quarter the size of the original wedge + bunny
  4. The grey (darkgrey) rectangle has a width of 330 and height of 70
  5. This set of slides gives a high-level overview of how to make bunny money
  6. Here is bunnyMoney shown with a gridlines spaced 50 apart and also with gridlines spaced 25 apart (use the grids to figure out where to place things):

Bunny Money Mosaic information that will be useful for you:

  1. The pink canvas has dimensions 900x600
  2. The 900x600 bunny money mosaic with grid is shown here:
  3. Note that the ears of the bunny are always outlined in white; sometimes the bunny is scaled smaller and the ears may appear to be non-white, but rest assured they retain their original color.

Task 3: Fancy Bunny Money [THIS IS AN OPTIONAL TASK]

If you had fun making bunny money and have time to spare, then you can complete this OPTIONAL task. In this OPTIONAL task, the goal is to create this picture in the file fancyBunnyMoneyMosaic.py:

The image is very similar to the one in Task 2 above, but has a line of bunnies in the grey rectangle.

Useful fancy bunny money tips:

  1. Here is an enlarged view of fancy bunny money with a grid (each grid line is 50 units apart):
  2. The smaller bunnies are 30% the size of the original bunny; the larger bunnies are 42% the original size.
  3. Here is an enlarged closeup of the line of bunnies with a grid (each grid line is 10 units apart):
    .
    This gives information about where to place each bunny.

Task 4: Honor Code Form

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.