Problem Set 6 - Due Tuesday, April 7, 2020
- Slides and notebooks from Lec 09 (Iteration 1), Lec 10 (Lists, Memory Diagrams, and Mutable vs. Immutable Sequences), Lec 11 (List Processing Patterns and List Comprehensions), and Lec 12 (Testing and Debugging)
- Problems and solutions from Lab 06 (Lists), Lab 07 (List Comprehensions), and Lab 08 (Testing and Debugging).
- Think Python, Ch. 8: Iteration
- Think Python, Ch. 10: Lists
About this Problem Set
This problem set will give you practice with lists,
list comprehensions, testing, and debugging.
- Task 0 is the usual individual problem involving puzzles based on the problem set 5 solutions. Because we provide you with a solution file for task 1, this task 0 only covers PS05 task 2.
- In Task 1 (individual task), you will define functions that create lists through accumulation and list comprehensions. This is a continuation of the Word Play task from PS05.
- In Task 2 (partner-recommended task), you will use a
whileloop to implement a rock-paper-scissors game with multiple rounds, extending the Rock Paper Scissors task from PS04.
- There are some extra challenge problems at the end if you want more coding practice.
In Task 2, having a partner is optional, but recommended. If you want to find a partner, use this Piazza post.
The CS111 Problem Set Guide gives an overview of psets, including a detailed description of individual and partner tasks. It has updated instructions for working with a partner remotely.
Follow the style guidelines in the CS 111 Code Style Guide
Based on data from Fall 2019, the tasks for this assignment took most students 5–7 hours (2–4 hours per task), plus 1 hour of reading time. Because that is longer than appropriate for a 1-week assignment, we have made some adjustments which will hopefully keep each task closer to 2–3 hours, including a new task 2 that is now a partner task rather than an individual task. We have also cut task 3 to shorten things further given the challenges of remote instruction.
When you've been working on a task for about 2 hours, you should evaluate whether you are making efficient progress, and make use of some of the class resources available to you, like help-room hours or office hours. If you've been working on one task for 3 hours and still have a ways to go, you should definitely get some help with it. We have tweaked the assignment in a way that will hopefully slightly reduce the time required.
- When you're done with the tasks, fill out your time spent form and follow the submission instructions to submit your pset.
All code for this assignment is available in the
ps06 folder in
cs111/download directory within your
cs server account.
Task 0: Scrambled Solutions
This is an individual problem which you must complete on your own, although you can ask for help from the CS111 staff.
This time we have only one puzzle for you to solve to review the solution to problem set 5 task 3. Go to:
and select the option under
Problem Set 5.
As before, please download and submit your solution files, and email
email@example.com if you run into trouble.
Task 1: Word Search
This task is an individual problem which you must complete on your own, though you may ask for help from the CS111 staff.
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.
Subtask 1a: Mapping, Filtering, and Accumulating with Loops
This task is a continuation of Task 1 from PS05.
In that task, you wrote the functions that included
You will use these functions to write three new functions that use a list of English words to find and return words that fulfill a certain property, stored in lists and tuples.
listBeauteousPrecariousWordsreturns a list of all English words of a specified length that are both beauteous (contain three consecutive vowels) and precarious (contain exactly one of each of the five vowels, not necessarily in order).
listGoodScrabbleWordsreturns a list of pairs (2-tuples) of words and their Scrabble scores for all English words whose Scrabble score is a above a given threshold.
bestScrabbleWordfinds the best-scoring Scrabble word in the English word list and returns a single pair (a 2-tuple) of this word and its Scrabble score.
Fill out the body of these functions in the file
following the contracts in that file.
Notice that the file imports the functions from
wordplay.py as well
as a list of words from
vocabulary.py. This list is called
englishwords, and is the list that your functions must search
through. Do not try to open
vocabulary.py in Canopy, because the list
is so large that it may cause your Canopy to freeze up for a bit.
In : listBeauteousPrecariousWords(8) Out: ['sequoias'] In : listBeauteousPrecariousWords(9) Out: ['behaviour', 'facetious', 'nefarious', 'tenacious', 'veracious', 'vexatious'] In : listBeauteousPrecariousWords(10) Out: ['abstemious', 'bivouacked', 'gregarious', 'mendacious', 'precarious'] In : listBeauteousPrecariousWords(14) Out:  In : listGoodScrabbleWords(35) Out: [('acquaintanceships', 35), ('compartmentalized', 35), ('compartmentalizing', 36), ('cryptographically', 35), ('czechoslovak', 35), ('czechoslovakia', 37), ('czechoslovakian', 38), ('czechoslovakians', 39), ('czechoslovaks', 36), ('electroencephalograph', 36), ('electroencephalographs', 37), ('embezzlement', 36), ('embezzlements', 37), ('extemporization', 35), ('extemporizations', 36), ('jazzily', 35), ('mozambiquean', 36), ('mozambiqueans', 37), ('overcapitalizations', 35), ('psychoanalytically', 36), ('psychokinetically', 36), ('psychopathically', 36), ('quinquagesimas', 35), ('quizzed', 35), ('quizzical', 38), ('quizzically', 43), ('quizzing', 36), ('schizophrenically', 41), ('schizophrenics', 35), ('sympathizingly', 37)] In : bestScrabbleWord() Out: ('quizzically', 43)
In the past, we have written
from turtle import *, which means get everything from the
turtlemodule. This time, we are only importing the particular named values that will be used in
from vocabulary import englishwords from wordplay import isBeauteous, isPrecarious, scrabbleScore
These functions use various list processing patterns:
listBeauteousPrecariousWordsfunction uses the filtering pattern introduced in Lec (List Processing Patterns and List Comprehensions)
listGoodScrabbleWordsfunction combines the mapping and filtering patterns from that lecture
bestScrabbleWordfunction uses the accumulating-a-value-from-a-list pattern
- This function must discover the maximal scrabble word score. You are not allowed to hardwire this score into your code.
- For simplicity, you may assume there is only one word with the maximal score.
In this subtask you must use loops (and not list comprehensions) to implement the four functions
- These four functions are tested in the testing code at the end of
wordSearch.py, but the testing we provide is more limited than in the past: you will need to do some of your own testing beyond what is already in the file.
Task 2: Rock-Paper-Scissors Rounds
The task 2 rubric shows how this task will be graded, and can be used as a checklist to make sure you are done with the task.
Now that we know how to write
while loops, your task is to build on the
code from PS04 task 1 to implement a
rock-paper-scissors game where the user plays against the computer until
they either win or lose a certain number of times (we have included
solution code for ps04 task 1 in the
rpsRounds.py starter file).
Here is a transcript of what the correct output looks like:
In: playRPS(3) Playing rock-paper-scissors (to 3 wins) Each round, pick a gesture from 'rock', 'paper', or 'scissors'. --- Round 1 (0 wins, 0 losses, and 0 ties) What gesture will you use? rock Computer chooses scissors You win! --- Round 2 (1 win, 0 losses, and 0 ties) What gesture will you use? paper Computer chooses paper Game is a tie! --- Round 3 (1 win, 0 losses, and 1 tie) What gesture will you use? scis Computer chooses rock Opponent wins! --- Round 4 (1 win, 1 loss, and 1 tie) What gesture will you use? r Computer chooses rock Game is a tie! --- Round 5 (1 win, 1 loss, and 2 ties) What gesture will you use? zzz Sorry, your input 'zzz' could not be interpreted as a gesture. Enter one of 'rock', 'paper', or 'scissors' or a prefix like 'r' or 'sci'. What gesture will you use? HAHAHA Sorry, your input 'HAHAHA' could not be interpreted as a gesture. Enter one of 'rock', 'paper', or 'scissors' or a prefix like 'r' or 'sci'. What gesture will you use? R Computer chooses paper Opponent wins! --- Round 6 (1 win, 2 losses, and 2 ties) What gesture will you use? PAPER Computer chooses rock You win! --- Round 7 (2 wins, 2 losses, and 2 ties) What gesture will you use? s Computer chooses scissors Game is a tie! --- Round 8 (2 wins, 2 losses, and 3 ties) What gesture will you use? r Computer chooses scissors You win! --- Congratulations, you won 3 times. Your record: 3 wins, 2 losses, and 3 ties.
Here is another transcript showing just the output produced by the
In : getGesture() What gesture will you use? lizard Sorry, your input 'lizard' could not be interpreted as a gesture. Enter one of 'rock', 'paper', or 'scissors' or a prefix like 'r' or 'sci'. What gesture will you use? science Sorry, your input 'science' could not be interpreted as a gesture. Enter one of 'rock', 'paper', or 'scissors' or a prefix like 'r' or 'sci'. What gesture will you use? sci Out: 'scissors'
Note: these transcripts include the extra blank lines printed by Canopy
input call; there are no blank lines printed intentionally
by the program itself. You don't have to worry about this detail, because
we will ignore blank lines when checking your output.
Your task is to complete the definitions of two functions in the
getGesture, which uses a
whileloop to ask the user for a gesture string until they enter one that's valid.
playRPS, which uses a
whileloop to let the user play against the computer until a certain number of wins is reached by one side.
Because the number of moves cannot be determined in advance (some rounds of the game might result in ties), you must use a
whileloop rather than a
forloop to implement the main game loop.
The starter file
rpsRounds.pycontains solution code to the PS04 Rock/Paper/Scissors task, and you should use those functions in several places. Note that the
playComputerfunction has been modified to return a string that indicates whether the result was a win, loss, or tie for the human player.
For full creditIf you'd like to challenge yourself, you must do the following (but you can ignore these details and still submit a decent attempt):
For 100% correct output, you will need to use the provided
stateSummaryfunctions within your
playRPSfunction so that, for example, if there is only 1 win, it says "1 win" instead of "1 wins". You should also have correct pluralization of the number of wins needed reported at the start of the game and the number of wins/losses reported at the end of the game. But only worry about this after you complete the core goals.
The user should be allowed to enter any prefix of a valid gesture, not just an exact gesture string, and case shouldn't matter.
You must match the strings printed in the game exactly. The example above shows a game in which the user wins; if the computer wins, the output should look like this (perhaps with different numbers and pluralization):
--- Too bad, you lost 3 times. Your record: 2 wins, 3 losses, and 3 ties.
You should interactively test the game in Canopy with different parameters. Behavior when the argument to
playRPSis 0 or not a positive integer is undefined, which means you don't have to care what your function does in those cases and we won't test them.
- To make testing easier, you can temporarily change the definition of
randomGestureso that it always returns the same value. Make sure that you remember to change it back when you're done testing though!
Feedback: Time Spent File
As in the previous psets, your time spent submission for this pset will
involve entering values for the variables in the
How to turn in this Problem Set
- Save your task 0 solution
.jsonfile in your
- Save your final
- Each team member must save their
rpsRounds.pyfile in their own
- Save your filled out
Note: It is critical that the name of the folder you submit is
ps06, and your submitted files are
In other words, do not rename the folder that you downloaded, do not create new files or folders, and do not delete or rename any of the existing files in this folder. We have automated scripts to check your electronic submission: an improperly named folder or a folder with improperly named files will not count as a valid submission. Improperly named files or functions will incur penalties.
Drop your entire
ps06folder in your
dropfolder on the
csserver using Cyberduck by 11:59pm on the DUE DATE = Tuesday April 7, 2020. If you need an extra 24 hours to turn it in, fill out the extension form before the deadline, and if you need more time than that, reach out to an instructor.
- As long as you submit an honest attempt by the deadline, you will get credit for your work on this problem set, even if it is not perfectly complete or functional.
Challenge Problems (optional & ungraded)
Challenge 1: Mapping and Filtering with List Comprehensions
For this challenge, define two functions named
behave exactly like the correspondingly named functions from subtask
1a. The difference is that the function definitions for this
challenge must use list comprehensions rather than explicit
loops to perform all mapping and filtering on lists.
LCstands for List Comprehension.
Each of your three
LCfunction definitions for this challenge must have a function body containing exactly one statement: a
returnstatement that contains a list comprehension expressions. For example,
listBeauteousPrecariousWordsLCmust have exactly the form:
def listBeauteousPrecariousWordsLC(length): """ Returns a list of all english words that are exactly length letters long, contain at least three consecutive vowels, and contain exactly one of each of the five vowels (not necessarily in order). In the body of this function, you must use a list comprehension to process all the words. """ return ... your list comprehension goes here ...
- These three
LCfunctions are tested in the testing code at the end of
wordSearch.py, but testing is limited. You should add more tests on your own.
Challenge 2: Replacing Words
For this challenge, implement a function called
takes three arguments: (1) an arbitrarily long string containing
sentences, paragraphs, etc.; (2) a "target" word to be replaced; and (3) a
funny word with which to replace the "target" word. The function must return
a version of the first string where every occurrence of the "target" word
is replaced by the funny word. Extend the function to take a list of
target/replacement word pairs and rewrite the original string to replace
all occurrences of each target word with its corresponding replacement
word in this list of pairs.
Super-Challenge: Funny Replacements
If you pick replacement words for a text without knowing what it is (but constrained by correct parts-of-speech), the result is often hilarious, especially with classic texts.
For example, enter a verb, a noun, a place, an event, and an adjective in the boxes below, and then click on "show results" to see those words substituted.
Enter a past-tense verb (e.g., "programmed" or "created"):
Enter a plural noun (e.g., "chairs" or "turtles"):
Enter a definite place (e.g., "Science Center" or "backpack"):
Enter an event (e.g., "my appointment" or "the big game"):
Enter an adjective (e.g., "blue" or "soft"):
This is Just to Say (original version) I have eaten the plums that were in the icebox and which you were probably saving for breakfast Forgive me they were delicious so sweet and so cold (by William Carlos Williams 1962)
This is Just to Say (your version) I have VERBED the NOUNS that were in the PLACE and which you were probably saving for EVENT Forgive me they were ADJECTIVE so sweet and so cold (by William Carlos Williams 1962 and YOU, right now)
It turns out that you already know everything you need to know to
implement this in Python: you can use
input to ask the user to enter
things, and then something like the code from the previous challenge to
put the words the user enters into a paragraph of text, and display that
text to the user.
We have no assessment for this, but if you're looking to practice your programming skills a bit more, try to create a program in a new file that implements this word game. You will have to figure out yourself how to do that, and make some of your own choices about how exactly it should work, which is good exercise in designing programs instead of just implementing them.