Homework 6: Much Ado About Lists!

Handin

After completing the homework, you will submit:

Hand in your work at this Google Form (https://goo.gl/forms/Fkr461qrsm27iZnP2)

The Assignment

This assignment draws on some basic ideas about analyzing text. Modern text analysis would use tables and statistics as part of its interpretation. We are doing less of that (since our focus is on list programs this week), but the first few questions are motivated by the text-analysis context.

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 writing in lecture. Check for italicized notes under each list-processing function for instructions on this.

Your solutions should include a list-of-tasks in a comment at the top of your answers for each of problems 2 through 5.

Setup

Go to this Google Doc and copy and paste everything into your document. This gives you a definition for a String called book-text. It contains the text of the first act of Shakespeare’s Much Ado About Nothing, which is Nicole’s favorite play. Note that the contents of this google doc include a line that is massively over 80 characters. Don’t worry about this: keep the code you write under 80 characters per line, but don’t worry about it for the given text.

A literary endeavor

Put your answers to 1-6 in a file called shakespeare.arr.

Converting Text to a List of Words

  1. Nicole notices that she needs to examine individual words in the text to analyze them. Split book-text into a list of words, using the builtin string-split-all (in the Pyret Strings documentation). Name the resulting list of words/strings split-text.

Identifying Characters

  1. Nicole 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, we want a function that takes a list of Strings representing a play and returns a list of Strings that contains the names of all characters in the play, without duplicates. The order of characters doesn’t matter.

    NOTE: 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-charactersO (operator version – last char is letter O not zero) and find-charactersR (recursive version).

Tallying Lines Per Character

The ALL-CAPS names mark each character’s spoken lines in the text. Nicole wants to determine how many lines each character has, while also setting up her results for use in a later analysis.

  1. Write a function count-characters 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 argument to your function. The count column should contain the number of times that character’s name occurred in ALL-CAPS.

    To construct the table here, use the following two functions, which are in the cs111-2018 file:

    • create-table-with-col(colname :: String, colvals :: List) -> Table
    • add-col :: (t :: Table, name :: String, colvals :: List<Any>) -> Table

    The first one creates a table with one column. The second adds a column to an existing table. For add-col, the number of values in the colvals argument must equal the number of rows in 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

  1. 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 builtin num-round). Include your task-plan as a comment before your code for this question.

    NOTE: you may solve this problem however you like, with recursion, built-in operations, or a mix of the two.

Where’s Waldo??

  1. We’ve hidden “Waldo” somewhere in the “Much Ado About Nothing” text. Write a function find-waldo which takes a single string of text as input and returns the word that occurs immediately after “Waldo” in that text.

    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.

    NOTE: Solve this problem using recursion rather than built-in operators.

  2. For the last 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? You do NOT have to write the code for this version, but if you want to give it a try you are welcome to do so (call it find-waldo-2).

Waldo’s Snacks

Put your answers to these questions in fruits.arr.

Material for this question will be covered on Friday

Once Nicole 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. Nicole decides to build a datatype to help him track his food supplies.

  1. Create a datatype for a FoodItem. There is one constructor food that takes in a food-name (String), a days-until-expiration (Number) and food-group (String). The food-group will be something like “fruit”, “vegetable”, “grain”, “dairy”, etc.

    Also create a list ofFoodItems called food-list, containing three foods you think Waldo might enjoy (your choice).

  2. Finally, write a function is-expired that takes one FoodItem and returns a boolean indicating whether the days-until-expiration is below zero.

Grading and Expectations

We’ll be following the usual guidelines on design, code clarity, functionality, and testing. We will also look for the task lists for questions 2-5.