Problem Set 6 - Due Tuesday, April 7, 2020

Reading

  1. 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)
  2. Problems and solutions from Lab 06 (Lists), Lab 07 (List Comprehensions), and Lab 08 (Testing and Debugging).
  3. Think Python, Ch. 8: Iteration
  4. Think Python, Ch. 10: Lists

About this Problem Set

This problem set will give you practice with lists, for and while loops, list comprehensions, testing, and debugging.

In Task 2, having a partner is optional, but recommended. If you want to find a partner, use this Piazza post.

All code for this assignment is available in the ps06 folder in the 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:

CS 111 Puzzles

and select the option under Problem Set 5.

As before, please download and submit your solution files, and email pmawhort@wellesley.edu 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 isBeauteous, isPrecarious, and scrabbleScore.

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.

Fill out the body of these functions in the file wordSearch.py 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.

Sample Executions

In [1]: listBeauteousPrecariousWords(8)
Out[1]: ['sequoias']

In [2]: listBeauteousPrecariousWords(9)
Out[2]: ['behaviour', 'facetious', 'nefarious', 'tenacious', 'veracious', 'vexatious']

In [3]: listBeauteousPrecariousWords(10)
Out[3]: ['abstemious', 'bivouacked', 'gregarious', 'mendacious', 'precarious']

In [4]: listBeauteousPrecariousWords(14)
Out[4]: []

In [5]: listGoodScrabbleWords(35)
Out[5]: [('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 [6]: bestScrabbleWord()
Out[6]: ('quizzically', 43)

Notes


Task 2: Rock-Paper-Scissors Rounds

In this problem, having a partner is optional, but is recommended. If you want to find a partner, use this Piazza post. The pset guide has updated recommendations on working with a partner remotely.

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).

Target Output

Here is a transcript of what the correct output looks like:

In[1]: 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 getGesture function:

In [1]: 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[1]: 'scissors'

Note: these transcripts include the extra blank lines printed by Canopy before each 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

Your task is to complete the definitions of two functions in the rpsRounds.py file:

  1. getGesture, which uses a while loop to ask the user for a gesture string until they enter one that's valid.
  2. playRPS, which uses a while loop to let the user play against the computer until a certain number of wins is reached by one side.

Notes

Testing


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 timeSpent.py file.


How to turn in this Problem Set


Challenge Problems (optional & ungraded)

Challenge 1: Mapping and Filtering with List Comprehensions

For this challenge, define two functions named listBeauteousPrecariousWordsLC and listGoodScrabbleWordsLC that 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.

Notes

Challenge 2: Replacing Words

For this challenge, implement a function called keyboardLeopard that 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.

See here for more inspiration.

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"):

show results
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.