Homework 6: Much Ado About Lists!
Due: Tuesday March 10, 2020 at 9:00PM EST.

Handin
Due: Wednesday February 19, 2020 at 9:00PM EST.
Setup and Handin
Setup
- We are not using a template for this homework! Like with the first assignment, use code.pyret.org.
Handin
- Download your file and make sure it is called
hw6-code.arr
. Hand in your work on Gradescope, making sure to use your anonymous account.
Remember your resources!
The Assignment
This assignment draws on some basic ideas about analyzing text. For some of these problems, we will tell you whether to use the built-in list operations (like L.map
and L.filter
) or an explicit recursion function as we have been discussing in lecture.
A Literary Endeavor
The American West is a land of great discovery, and college is a time of spontaneous exploration. In pursuit of loot, Moses and Raj are investigating a ransacked saloon in Cody, WY when they stumble upon an old stone tablet. After some research, they find out that the tablet is a map to the long lost Wyoming Treasure! Help Moses and Raj crack the tablet’s text and find the secret treasure!
Task: Copy and paste this code into your file.
include shared-gdrive("shakespeare-text.arr", "1-XGIpbC13ItjxCh7WY14cCjfygk5wz5x")
include shared-gdrive("cs111-2019.arr", "1YNg3-qHTjhd7koZVffPU6t032QX8vryg")
import lists as L
import math as M
book-text
Converting Text to a List of Words
Raj notices that the text on the tablet is from the first act of Shakespeare’s Much Ado About Nothing. This text is named book-text
in the stencil code. You can see the text in full here. Raj needs your help to examine individual words in the text to analyze them for clues.
Task: Split book-text
into a list of words, using the builtin string-split-all
(in the Pyret String
documentation). Name the resulting list of words/strings split-text.
Identifying Characters
Raj wants to compute the names of all the characters in the play. In the text, only the names of characters are in ALL CAPS. To find which of the words in split-text
are characters’ names, we want a function that takes a list of Strings
representing a play and returns a list of every String
which is a name of a character in the play, without duplicates. The order of characters doesn’t matter.
Task: Write this function two ways, once using only built-in list operators and once with explicit recursion to locate the names (you can still use L.distinct
as part of your recursive version). Name your versions find-charactersB
(built-in version) and find-charactersR
(recursive version).
Tallying Lines Per Character
The ALL-CAPS names mark each character’s spoken lines in the text. Raj wants to determine how many lines each character has, while also setting up his results for use in a later analysis.
Task: Write a function characters-table
which takes in a list of Strings
representing the names of characters in a play, and a list of Strings
representing the text of the play. This function should return a Table
with two columns: name
and count
. The name
column should contain every character’s name in the order it was listed in the first input to your function. The count
column should contain the number of times where that character’s name occurred.
Note: Do not use the built-in Table
function count
.
To construct the table here, use the following two functions, which are in the file you included at the top:
-
create-table-with-col(colname :: String, colvals :: List) -> Table
- This function creates a
Table
with one column.
-
add-col :: (t :: Table, name :: String, colvals :: List<Any>) -> Table
- This function adds a column to an existing table. The number of values in the
colvals
input must equal the number of rows in t
, the Table
(they will be added in order).
The following example shows how to build a table of room seating capacity:
new-table = create-table-with-col("room", [list: "CIT 101", "CIT 368"])
add-col(new-table, "seats", [list: 10, 65])
Note: you may solve this problem however you like, with recursion, built-in operators, or a mix of the two.
Average word length
Task: Write a function average-word-length
which takes in a list of Strings
and returns the average length of all words in the list, rounded to the nearest integer (use the built-in num-round
).
Note: you may solve this problem however you like, with recursion, built-in operations, or a mix of the two.
Where’s Waldo??
Moses got a secret clue! “Waldo” is hidden somewhere in the “Much Ado About Nothing” text. But the clue doesn’t contain any hints about where to find Waldo.
Task: Write a function find-waldo
which takes a String
of text as input and returns the word that occurs immediately after “Waldo” in that text. Solve this problem using recursion rather than built-in operators.
For example, if the String
is “the evening breeze Waldo rustled the leaves on the balcony”
, then your function should return "rustled"
. It should always return the word immediately after Waldo. You may assume that “Waldo” will always be present exactly one time and it will not be the last word of the list.
For the most recent question, we only asked you to find the word directly after Waldo, but we might also have asked you to find the words directly before and after him (for our example above, this result would have been [list: "breeze", "rustled"]
). What makes that version of the problem more challenging given what we’ve learned so far? What ideas do you have about how you might do that if we asked?
Task: Add a block comment underneath find-waldo
answering these questions.
Waldo’s Snacks
Material for this question will be covered later this week.
Once Moses finds Waldo, he reveals that he has been hiding among the rustling leaves for years, waiting for someone to find him, and he is very hungry. Moses has a bag of mystery food supplies, but he doesn’t know which are good to give Waldo. He decides to build a datatype to help him track his food supplies and decide what to give Waldo.
Task: Create a datatype FoodItem
. There is one constructor food
that takes in food-name
(a String
), days-until-expiration
(a Number
) and food-group
(a String
). The food-group
will be something like “fruit”, “vegetable”, “grain”, “dairy”, etc.
Task: Create a list ofFoodItems
called food-list
, containing three foods you think Waldo might enjoy (your choice).
Finally, write a function is-expired
that takes one FoodItem
and returns a boolean indicating whether the days-until-expiration
is below zero.

Theme Song
I’m an Old Cowhand by Sonny Rollins
Brown University CSCI 0111 (Spring 2020)
Do you have feedback? Fill out this form.
Homework 6: Much Ado About Lists!
Due: Tuesday March 10, 2020 at 9:00PM EST.
Handin
Due: Wednesday February 19, 2020 at 9:00PM EST.
Setup and Handin
Setup
Handin
hw6-code.arr
. Hand in your work on Gradescope, making sure to use your anonymous account.Remember your resources!
Table
documentation)The Assignment
This assignment draws on some basic ideas about analyzing text. For some of these problems, we will tell you whether to use the built-in list operations (like
L.map
andL.filter
) or an explicit recursion function as we have been discussing in lecture.A Literary Endeavor
The American West is a land of great discovery, and college is a time of spontaneous exploration. In pursuit of loot, Moses and Raj are investigating a ransacked saloon in Cody, WY when they stumble upon an old stone tablet. After some research, they find out that the tablet is a map to the long lost Wyoming Treasure! Help Moses and Raj crack the tablet’s text and find the secret treasure!
Task: Copy and paste this code into your file.
Converting Text to a List of Words
Raj notices that the text on the tablet is from the first act of Shakespeare’s Much Ado About Nothing. This text is named
book-text
in the stencil code. You can see the text in full here. Raj needs your help to examine individual words in the text to analyze them for clues.Task: Split
book-text
into a list of words, using the builtinstring-split-all
(in the PyretString
documentation). Name the resulting list of words/stringssplit-text.
Identifying Characters
Raj wants to compute the names of all the characters in the play. In the text, only the names of characters are in ALL CAPS. To find which of the words in
split-text
are characters’ names, we want a function that takes a list ofStrings
representing a play and returns a list of everyString
which is a name of a character in the play, without duplicates. The order of characters doesn’t matter.Task: Write this function two ways, once using only built-in list operators and once with explicit recursion to locate the names (you can still use
L.distinct
as part of your recursive version). Name your versionsfind-charactersB
(built-in version) andfind-charactersR
(recursive version).Tallying Lines Per Character
The ALL-CAPS names mark each character’s spoken lines in the text. Raj wants to determine how many lines each character has, while also setting up his results for use in a later analysis.
Task: Write a function
characters-table
which takes in a list ofStrings
representing the names of characters in a play, and a list ofStrings
representing the text of the play. This function should return aTable
with two columns:name
andcount
. Thename
column should contain every character’s name in the order it was listed in the first input to your function. Thecount
column should contain the number of times where that character’s name occurred.Note: Do not use the built-in
Table
functioncount
.To construct the table here, use the following two functions, which are in the file you included at the top:
create-table-with-col(colname :: String, colvals :: List) -> Table
Table
with one column.add-col :: (t :: Table, name :: String, colvals :: List<Any>) -> Table
colvals
input must equal the number of rows int
, theTable
(they will be added in order).The following example shows how to build a table of room seating capacity:
Note: you may solve this problem however you like, with recursion, built-in operators, or a mix of the two.
Average word length
Task: Write a function
average-word-length
which takes in a list ofStrings
and returns the average length of all words in the list, rounded to the nearest integer (use the built-innum-round
).Note: you may solve this problem however you like, with recursion, built-in operations, or a mix of the two.
Where’s Waldo??
Moses got a secret clue! “Waldo” is hidden somewhere in the “Much Ado About Nothing” text. But the clue doesn’t contain any hints about where to find Waldo.
Task: Write a function
find-waldo
which takes aString
of text as input and returns the word that occurs immediately after “Waldo” in that text. Solve this problem using recursion rather than built-in operators.For example, if the
String
is“the evening breeze Waldo rustled the leaves on the balcony”
, then your function should return"rustled"
. It should always return the word immediately after Waldo. You may assume that “Waldo” will always be present exactly one time and it will not be the last word of the list.For the most recent question, we only asked you to find the word directly after Waldo, but we might also have asked you to find the words directly before and after him (for our example above, this result would have been
[list: "breeze", "rustled"]
). What makes that version of the problem more challenging given what we’ve learned so far? What ideas do you have about how you might do that if we asked?Task: Add a block comment underneath
find-waldo
answering these questions.Waldo’s Snacks
Material for this question will be covered later this week.
Once Moses finds Waldo, he reveals that he has been hiding among the rustling leaves for years, waiting for someone to find him, and he is very hungry. Moses has a bag of mystery food supplies, but he doesn’t know which are good to give Waldo. He decides to build a datatype to help him track his food supplies and decide what to give Waldo.
Task: Create a datatype
FoodItem
. There is one constructorfood
that takes infood-name
(aString
),days-until-expiration
(aNumber
) andfood-group
(aString
). Thefood-group
will be something like “fruit”, “vegetable”, “grain”, “dairy”, etc.Task: Create a list of
FoodItems
calledfood-list
, containing three foods you think Waldo might enjoy (your choice).Finally, write a function
is-expired
that takes oneFoodItem
and returns a boolean indicating whether thedays-until-expiration
is below zero.Theme Song
I’m an Old Cowhand by Sonny Rollins