Due: Tuesday, July 10 at 6pm
To practice writing programs over tables
Collaboration Policy: This is an individual assignment. Your work must be your own, though you can certainly utilize Kathi and Eli’s office hours for help.
Reminder: put the following lines at the top of your file to get all the needed table operations:
include tables |
include shared-gdrive("table-functions.arr", "14jG4wvAMhjJue1-EmY-u9hX4UwmPHCO8") |
include gdrive-sheets |
Put your work for this set of exercises in a file named icecream.arr. At the top of the file, confirm that you worked on your own by including this statement: "I understand this is an individual assignment, the work below is my own"
A local ice cream store stores information about its current flavors in a Google Sheet. It wants to use data from the sheet to generate the menu for its new digital menu board. Your job is to write the programs they need to generate content for the menu board.
Here’s the link to the Google sheet. Create a Pyret file for this assignment, and load this sheet as your initial table. We’ll refer to this as the flavors table in describing subsequent problems.
Loading a Table from Google Sheets: the following lines show you how to load the above table into Pyret:
include gdrive-sheets |
|
# the long string below is the last part of the URL to the sheet |
flavors-sheet = load-spreadsheet("1drj1kiC3VVCe17wrrTpNUn0DSx9R0mhdNkan7znOdcE") |
|
flavor-data = load-table: name, core, add-in, base, style |
source: flavors-sheet.sheet-by-name("flavors", true) |
end |
The Problems
Imagine that you were asked to find the rows from the flavor-data table in which either the "Core Flavor" or "Add In" columns contain chocolate. Which rows should be included? Write a comment listing the flavor names whose rows should be in the output. No code is required for this question.
Write an expression that performs the computation from the previous question. Specifically, define a table named has-chocolate that contains the rows from the flavor-data table in which either the "Core Flavor" or "Add In" columns contains chocolate. Pyret’s string-contains function is useful here: it takes two strings and determines whether the first string contains the second as a substring (try it out in the interactions window to learn how it works)
Did you get the answer that you expected from question 1? If not, what was different? (You don’t have to have matched your expected answer necessarily – we’re asking you to think about expectations and results. You don’t lose points for your answers to these two questions being different as long as you had reasonable answers in each case and can articulate any differences between them.
Clearly, the ice cream folks are too busy scooping to keep their capitalization consistent. Two possible solutions here are to modify your has-chocolate program to look for either "chocolate" or "Chocolate", or you could (somehow) transform the table you read in to convert all the core flavors to lowercase after you load the table. What are the advantages and disadvantages of each approach? Put your answer in a block comment in your Pyret file. Don’t write code, just discuss the approaches.
Define a table named clean-flavors that puts the strings in the "Core Flavor" and "Add In" columns all in lowercase. Generate this table via a program (don’t create a new table by hand).
Pyret has an operation called transform-column that calls a function on every cell in a given column. For example, if you had a function that added 5 to a number, the following expression would give all students 5 extra points on exam1 in our gradebook problem:
transform-column(gradebook, "exam1", add5)
Look in Pyret’s strings documentation to find a function that will help you convert to lowercase.
The ice cream store frequently gets visitors with dietary restrictions. They want to list flavors that are either vegan or nut-free in separate sections of the menu. Define a table named nut-free-vegan-menu that contains two new columns of booleans:
The first indicates whether the item is vegan (which means it has "almond milk" or "coconut milk" as its base and does not use "marshmallows" as its add-in)
The second new column indicates whether the item is free of nuts (which means it does not use "almond milk" as its base or "peanuts" as its add-in).
Generate this table via a program (don’t create a new table by hand).
In order to generate the menu for the digital display board, the ice cream shop needs not just the names of the flavors, but also a description of each flavor that indicates the core flavor, add-in, and style of the flavor. Here are examples of the descriptions they want to see:
"black raspberry ice cream with chocolate chips" (for either hard or soft serve ice cream)
"simple strawberry ice cream" (for an item with no add-in)
"mango frozen yogurt with papaya" (for frozen yogurt)
Define a table called descriptions that includes a new column with the descriptions of each item, following these examples. Generate this table via a program (don’t create a new table by hand).
It’s time to generate the flavor menu! The shop may put menus for different styles of ice cream in different sections of its display, so it wants to be able to quickly generate a table of flavors that are available in a given style, with the rows sorted alphabetically by the name of the flavor.
Define a function flavor-by-style that takes a string for the style ("hard", "soft", or "froyo") and produces a table of the flavors from that style, sorted alphabetically by flavor.
Show how to use your function to get the tables for each style.
During a local vegan-eating festival, the owner wants to highlight the vegan options within each of the styles. You’ve now written some code to work with vegan options and some code to work with focusing on particular styles. Write an expression that produces all of the vegan hard-style rows.
In a comment, briefly discuss the extent to which you were able to reuse code you had already written, versus how much code you had to repeat. If you feel had to repeat code unnecessarily, describe how you might have set up the code differently up front to avoid the repetition.
You do NOT have to rewrite your code for the second part. Again, we are asking you to reflect on your work – what we are really measuring here are your observations and understanding, not whether your code was designed ideally the first time.
The ice cream shop has a new employee who will be in charge of maintaining the flavor spreadsheet. The programs you wrote for this assignment make certain assumptions about the data in the spreadsheet (how things are worded, for example). You want to give the new employee instructions on how to fill in the spreadsheet and what kinds of changes you need to know about so your programs continue to work as intended.
Provide a bullet list in a block comment as your answer. Each bullet item should contain a brief/one-sentence summary of the issue and a concrete example of a cell content that would cause a problem (indicate which column the cell is in).
We are NOT asking you to modify your code to work around these issues. This question simply checks whether you are able to think about what the issues might be. We have more to learn before we can begin to address potential issues here.
Put your work for this set of exercises in a file named gradebook-hwk.arr. Include only the code needed for this assignment (not all of the code from your notes from this week). Confirm that you worked on your own by including this statement: "I understand this is an individual assignment, the work below is my own"
At the end of Thursday’s lecture, we were left with the following function (or one similar) to compute letter grades:
fun grade-to-letter(r :: Row) -> String: |
doc: "produce a letter grade for a student" |
if r["avg"] >= 88: "A" |
else if (r["avg"] >= 76) and (r["avg"] < 88): "B" |
else if (r["avg"] >= 60) and (r["avg"] < 76): "C" |
else: "NC" |
end |
end |
The current function does not properly handle students who are taking the course SNC. These students should have a grade of "S" if they would have earned either an "A", "B", or "C" according to the grade-to-letter function. Edit this definition to also handle grades for students taking the course SNC.
A professor who is using our gradebook program wants to be able to look at students whose grades lie within a range of values. For example, the professor wants to run the expression:
grade-range(gradebook, "exam1", 50, 70)
to get a table of all students whose score on exam1 is within the range 50-70 (inclusive). The professor could use any numeric column in place of "exam1".
Write grade-range. You may assume that all column names exist in the table and contain numeric data. You may assume that the two numbers are given in order with the lower number first.
How would you write a where: block with examples of the grade-range function? Create such a block and include two examples/tests of the function.
Here are two versions of a function to add letter grades to a gradebook:
fun add-letter-grades1(to-gradebook :: Table) -> Table:
sort-by(
build-column(
build-column(to-gradebook, "avg", exam-avg),
"letter", grade-to-letter),
"letter", true)
end
fun add-letter-grades2(to-gradebook :: Table) -> Table:
avg-grades = build-column(to-gradebook, "avg", exam-avg)
with-letters = build-column(avg-grades, "letter", grade-to-letter)
sorted = sort-by(with-letters, "letter", true)
sorted
end
Answer the following questions within a comment block:
Do these two compute the same answer if they are given the same table? Why or why not?
If you were to put these into your file and Run it, call add-letter-grades2 in the interactions window, then ask for with-letters in the interactions window (at the prompt), what result would you get from Pyret? What about the ways that names are managed in Pyret leads to that result?
Discuss the strengths and weaknesses of each version. Do you have a preference? Which one and why?
For questions that ask you to write code, you’ll be graded on (a) whether the code performs the expected computation, (b) whether you have a reasonable set of examples on functions (in a where block), and (c) whether your code as good structure and style. For style, we want to see doc strings on functions, types on columns and parameters, and useful names for any parameters or variables you create.
For questions that are more free-response, you’ll be graded on whether you can articulate a technical opinion in a precise manner. Good answers should state technical reasons, not just opinions like "this is better", or "I like it".
Remember to include the collaboration statement in each file.
Submit two files, icecream.arr and gradebook-hwk.arr, through this Google form.