Problem Set 5 - Due Friday March 9

(Files for Tasks 1 and 2 due at 23:59 EST, paper copy of Task 3 due in lecture on Friday March 9.)


  1. Slides and notebooks from Lec 08 (Sequences and Loops), Lec 09 (Iteration, Part 1), and Lec 10 (Lists, Memory Diagrams, and Mutable vs. Immutable Sequences).
  2. Problems and solutions from Lab 05: Loops and Lab 06: Lists
  3. Think Python, Ch. 8: Strings
  4. Think Python, Ch. 7: Iteration
  5. Think Python, Ch. 9: Lists

About this Problem Set

This problem set will give you practice with sequences (strings and lists), operations on sequences, loops on sequences, loops with graphics, and list memory diagrams.

  1. In Task 1 you will create your own function that uses loops to generate pictures with repeating elements from cs1graphics.
  2. In Task 2 (Recommended Partner task), you will create your own functions that use loops to analyze and manipulate strings. Use this shared Google Doc to find a pair programming partner and record your partner. Remember that you can talk with other people about high-level problem-solving strategies, but sharing code in an Honor Code violation.
  3. In Task 3, you will will draw memory diagrams to capture changes in lists, practicing the concepts of mutability and aliasing. Take a picture or make a copy of your solution so you can compare it to our solution before grading is finished..

Other notes:

All code for this assignment is available in the ps05 folder in the cs111/download directory within your cs server account. This assignment also uses the Codder program to help you do a final check for Tasks 1 and 2 before you submit.

Task 1: Concentric Circles

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

In this task, you will create a Python file named and define in it the three functions concentricCircles, showConcentricCircles, and circleRow.

Your file should begin with this header, which you should fill out:

# Your name:
# Your username:
# CS111 PS05 Task 1
# Submission date:

You should also import cs1graphics via the following line at the beginning of your code:

from cs1graphics import *

Subtask 1a: concentricCircles

In this subtask, your goal is to generate pictures like the following:

paper1 = Canvas(200, 200)
cybla = concentricCircles(200, 5, 'cyan', 'black')
cybla.move(100, 100)
paper2 = Canvas(400, 400)
grellow = concentricCircles(400, 8, 'green', 'yellow')
grellow.move(200, 200)
paper3 = Canvas(500, 500)
pima = concentricCircles(500, 2, 'pink', 'magenta')
pima.move(250, 250)
paper4 = Canvas(500, 500)
reblu = concentricCircles(500, 13, 'red', 'blue')
reblu.move(250, 250)

In, define the function concentricCircles that fulfills the following contract:

def concentricCircles(size, numCircles, color1, color2):
    """Creates and *returns* a Layer containing numCircles
       circles, all of which are centered at (0, 0).
       Let r be the radius of the smallest circle. Then the radii of
       the circles grow in arithmetic progression -- r, 2r, 3r, etc. --
       up to the radius of the largest circle, which is size/2.
       The smallest circle is filled with color1 and afterwards the colors are
       alternated between color1 and color2.



Subtask1b showConcentricCircles

If you look at the 4 statements (shown in gray boxes in Task 1a above) that are used to generate each of the concentric circles, you'll note there is a pattern. Capture that pattern by writing a function called showConcentricCircles in the file that fulfills the following contract:

def showConcentricCircles(canvasSize, numCircles, color1, color2):
    """Creates and *returns* a canvasSize x canvasSize white canvas containing
       numCircles concentric circles, with the smallest circle having color1
       and alternating with color2. The title of the canvas should
       be a string representation of the invocation of the showConcentricCircles
       function that created the canvas.

For example, the following invocation produces the canvas below (note the title):

    showConcentricCircles(500, 13, 'red', 'blue')


Subtask 1c: circleRow {task1c}

In this task, your goal is to generate pictures like the following:

single = circleRow(1, 500, 7, 'orange', 'yellow')
triple = circleRow(3, 400, 10, 'red', 'pink')
quint = circleRow(5, 240, 8, 'blue', 'green')
dozen = circleRow(12, 100, 10, 'black', 'white')

In your file, define a function circleRow that fulfills the following contract:

def circleRow(numCirclesInRow, size, numCircles, color1, color2):
    """Creates and *returns* a numCirclesInRow*size x size white canvas containing
       numCirclesInRow circles, each of which is a concentric circle (created
       by invoking concentricCircles). The distance between the center of each
       successive circle is size. The title of the canvas should be a string
       representation of the invocation of the circleRow function that
       created the canvas.



Task 2: Word Play

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

Subtask 2a: Word Properties

We are interested in some fun properties of written words.

  1. Does the word use only the top row of keys (i.e., 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p') on the keyboard? Examples are typewriter and pretty.
  2. Does a word contain at least three vowels in a row? Examples are beautiful and delicious.
  3. Does a word contain exactly one occurrence of each of the five vowels? Examples are education and tambourine.
  4. What is the Scrabble value of the word? This is determined by adding the points assigned by Scrabble to each letter, which are:
A = 1 B = 3 C = 3 D = 2 E = 1 F = 4
G = 2 H = 4 I = 1 J = 8 K = 5 L = 1
M = 3 N = 1 O = 1 P = 3 Q = 10 R = 1
S = 1 T = 1 U = 1 V = 4 W = 4 X = 8
Y = 4 Z = 10

Your task is to flesh out the definitions of the functions in the provided file Your functions should not be sensitive to capitalization (see examples below).

In [2]: isTopRow('typewriter') # topRow words use only top row keys
Out[2]: True

In [3]: isTopRow('Pretty')
Out[3]: True

In [4]: isTopRow('bunny')
Out[4]: False

In [5]: isTopRow('sequoia')
Out[5]: False

In [6]: isBeauteous('delicious') # Beauteous words have at least 
Out[6]: True                     # three consecutive vowels

In [7]: isBeauteous('sequoia') 
Out[7]: True

In [8]: isBeauteous('education') 
Out[8]: False

In [9]: isBeauteous('royal') 
Out[9]: False

In [10]: isPrecarious('education') # Precarious words have 
Out[10]: True                      # exactly one of each vowel

In [11]: isPrecarious('sequoia') # 'sequoia' is both beauteous *and* precarious!
Out[11]: True

In [12]: isPrecarious('auction') # No 'e'
Out[12]: False

In [13]: isPrecarious('precautionary') # Two 'a's
Out[13]: False

In [14]: scrabbleScore('juxtapose')
Out[14]: 25

In [15]: scrabbleScore('quiz')
Out[15]: 22

In [16]: scrabbleScore('Wellesley')
Out[16]: 15

In [17]: scrabbleScore("I'm a n00b!") # Nonletter characters have score 0
Out[17]: 9



Subtask 2b: Mystery Speech

What would English sound like if we were not allowed to use each letter more than once in a sentence? Would one still be able to understand it? Let's try it out with some famous (or not) phrases. Can you guess them? (The phrases are shown at the end of this subtask).

In []: keepFirstLetter(somePhrase1)
Out[]: 'To be r n  '
In []: keepFirstLetter(somePhrase2)
Out[]: 'The king s da, lo v  !'
In []: keepFirstLetter(somePhrase3)
Out[]: 'We ar nv  gti bck oh'
In []: keepFirstLetter(somePhrase4)
Out[]: 'Only Victr s kd xeg a pu f h j q   BMW   z'
In []: keepFirstLetter(somePhrase5)
Out[]: 'Quizes ar dfclt o py n wv xh, b g mk j.'

In this subtask, you'll flesh out the body of the function keepFirstLetter which transforms string phrases as shown above. This function is also part of the file.

def keepFirstLetter(phrase):
    """Returns a new string that contains only the first occurrence of a
       letter from the original phrase.
       The first letter occurrence can be upper or lower case.
       Non-alpha characters (such as punctuations and space) are left



Fun Ungraded Challenge: If you manage to create a meaningful English phrase which after passing through keepFirstLetter contains all the letters of the alphabet, please post it to the Google Group. We will add them to the mystery phrases for the next semester. *Credit to Riley Rettig and Jennifer Chang for creating somePhrase4; Alissa Tinney and Isabel Bryant for creating somePhrase5.

Task 3: Memory Diagrams

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

Take a picture or make a copy of your solution so you can compare it to our solution before grading is finished.

Consider the following sequence of nine statements that create and manipulate nested lists in Python:

r = [[3,4,5], [6,7], [8]]

s = r[0]

s[1] = 9

r[0][2] = r[1]


s[2][1] = r[0][0] + r[0][1]

s[2][2][0] = s[2][0] + r[1][1]

r.insert(0, r[1][2])

r[2][2] = 17

Your task is to draw a sequence of nine memory diagrams that show the state of Python's memory after each of the nine statements.


Task 4: Honor Code Form

As in the previous psets, your honor code submission for this pset will involve defining entering values for the variables in the file.

How to turn in this Problem Set

Hard-Copy Submission

Bring to lecture on Friday March 9 the paper copy of your Task 3. Include your name and section on your submission. Take a picture or make a copy of your solution so you can compare it to our solution before grading is finished.

Soft-Copy Submission