Due: Wednesday, February 22, 2023 at 11:59PM ET.
hw4-code.arr
where you will write your Homework 4 solution.hw4-written.pdf
. This will hold your written work and graphs.hw4-code.arr
:Make sure that your starter code has the line include data-source
instead of import data-source as ds
Table
documentation)This assignment has you practice a multi-stage data-analysis task. We're going to write functions that draw on data from two tables to perform computations. We'll also do multiple versions of a function, where each version adds more features, so you can see an example of building up solutions a bit at a time.
This assignment draws on all of the table lectures so far. For the planning tasks, we saw an example on 2/15, and you're also practicing it in lab 3. While we have covered everything that you need as of 2/15, the examples we do in class on 2/17 will give more practice with the concepts in this assignment.
Remember to use the CS0111 Table
documentation (linked in Setup and Handin), not the official documentation. If your code uses constructs named sieve or extend (without functions), for example, you are using the wrong version and will not get credit.
The Marvel universe has superheroes all over the globe. As an international organization, Marvel has to deal with the complexities of exchange rates. In order to do this, the leaders at Marvel have to manipulate tables containing complicated data. In fact, they do this in other instances like evaluating potentially stolen art and investigating the Titanic. This homework will get you ready to join Marvel by using your table skills to uncover important information.
Important: as you start working on more complicated programming problems, we want you to practice planning out your tasks before you start coding. Taking the time to plan your approach to the programming tasks can potentially save hours of debugging time. We have given you some guidance on planning here. We also want to learn how you plan, so that we can help you and students like you plan more productively in the future. As you work through the assignment, submit your plans for the tasks that say "(Submit a plan for this task!)" to this Google form (we are not grading these harshly – we want to see that you did them, and we want to observe the approach you took).
Learning Goals:
Companies that do business internationally need to be able to quote prices in different currencies, following a table of exchange rates. This is the kind of situation where one would have two separate tables: one with the prices of items, and another with the exchange rates per country. For this problem, we'll be computing currency conversions for an online art store in which artists and clients may be in different countries.
The store has two tables, that are both in your starter code. One is for artwork, called ART
: it tracks the unique id, cost and base currency for each piece, as well as what kind of artwork it is. It has some errors in the data, which we will clean up in the next part. It is commented out – ignore it for now.
The other table is for currency conversion, called CURRENCY-CONVERSION
. Each row holds the conversion rate (the multiplicative factor) to convert from the first currency (from-c
) to the second (to-c
).
For example, if a row of the Table
looked like:
we would convert from Euros (EUR) to Tunisian Dinars (TND) by multiplying the price in Euros by 3.32.
We will use these tables to develop several programs for selling art across currencies.
Tables
in this assignment look like?Unlike in Homework 3, these tables are not typed directly into Pyret, but rather pulled in from a Google Sheet. To see what these Tables
look like, feel free to type the name of the Table
in the definitions window and hit "Run", or hit "Run" first and then type the name in the interactions window and hit return.
For these tasks, you can use our CURRENCY-CONVERSION
Table
when writing tests.
Task 1: (Submit a plan for this task!) Write a function called exchange-price-1
, which takes in as input a Table
that has the same columns as CURRENCY-CONVERSION
, a starting currency (a String
), a desired currency (a String
), and a price (a Number
), and returns a Number
of the price converted from the starting currency to the desired currency. For this exercise, you can assume that a row with the starting currency in the "from-c" column and the desired currency in the "to-c" column exists exactly once in the table. For now, do not worry about handling the case of converting a currency to itself.
For this and every homework problem that has text like "you can assume that a row appears exactly once in the table," your test cases should obey this assumption. That is, for this problem, do not include tests like exchange-price-1(CURRENCY-CONVERSION, "JPY", "EUR")
, since this test violates the assumption (there are no rows with "JPY" in the "from-c" column and "EUR" in the "to-c" column in this Table
).
Remember that you can use mathematical expressions in tests, such as 0.25 is 1/4
.
Pyret also has an is-roughly
, but it will be much easier to use the mathematical expression instead.
Task 2: (Submit a plan for this task!) Sometimes, we cannot make the assumption that we make in Task 1. For this task, assume that input currency conversion table may not list the conversion from A to B, but it may list the conversion from B to A. Write a function called exchange-price-2
, which has the same inputs and output as exchange-price-1
, but uses the direct conversion (as in Task 1) or the inverse conversion if necessary. (You compute the inverse of a number N using 1 / N, where / is division).
Details:
Table
does not contain the relevant row. (Hint: what would the conversion rate be in this case?)Table
. If there are multiple matching rows, you can assume that they are consistent with each other (that is, if you have a Row
with A in to-c and B in from-c, and a row with B in to-c and A in from-c, no matter if you choose to do the direct conversion or the inverse conversion, you will get approximately the same answer.)Task 3: (Submit a plan for this task!) Create a function called plot-exchange-rates
which takes in a Table
that has the same columns as CURRENCY-CONVERSION
and a starting currency (a String
) and produces a bar chart of conversion rates from the given currency to all the other available currencies, that is, currencies for which there exists either a direct or an inverse conversion (or both) from the given currency.
This section deals with the ART
Table
. Most of the section is exploratory (you are meant to write short pieces of code and write down what you notice, but not turn in code), with one coding task that you turn in.
In the 2/13 lecture, we learned that there are multiple approaches to cleaning data: using sanitizers, manually cleaning up the source spreadsheet, filtering out rows that we recognize as malformed, and writing functions to clean up columns. One of your jobs in these tasks will be to determine which of these approaches makes the most sense for different kinds of messy data.
Task 4: Uncomment the block below the line that says # Uncomment for Part 2
by removing the surrounding #|
and |#
, and try to run the code. You will encounter one error. Using what you learned in class, change the load-table
block of code so that this error no longer happens (Note: column A corresponds to the "id" column). Even though the error text will hint towards editing the Google sheet itself, you can and should resolve this in code. In a one-sentence comment below this change, in your own words, explain what the error was and why it happened.
Now, examine the ART
Table
in the Pyret window. For the data to be considered "clean," we would want:
The next few tasks ask you to reflect on what it would mean to clean the existing data to conform to these expectations.
As you complete tasks 5-7 remember that, aside from scrolling through the columns to examine data, you can also graph the data or even sort it.
Task 5: One of the values in the "id" column appears twice, violating one of the requirements above. In a short comment that starts with "Part 2, Task 5:", name this value, and explain how to find it efficiently ("efficiently" here means faster than reading through all of the values in the column).
Task 6: In a short comment that starts with "Part 2, Task 6:", explain how the values in the "cost" column are not clean, using 1-2 specific examples from the data. Which of the approaches listed in the blue box at the top of this part would be your first choice for cleaning up the data, and why? Which would be your second choice, and what is an upside and a downside of this second choice?
Task 7: In a short comment that starts with "Part 2, Task 7:", explain how the values in the "currency" column are not clean, using 2-4 specific examples from the data. Which of the approaches listed in the blue box at the top of this part would be your first choice for cleaning up the data, and why? Which would be your second choice, and what is an upside and a downside of this second choice?
Task 8: Examine the "kind" column. We consider "visual" art to be art that has "paint" or "photo" in the "kind" description, and "sculpture" to be art that has "marble", "ceramic," or "wood" in the "kind" description (all case-insensitive). Everything else would be considered "other." Write a function called clean-kind
that takes in a String
(which you can assume will look like any of the values in the pre-cleaning "kind" column) and produces either "visual", "sculpture", or "other", based on the criteria named in this task. To test this task, use specific values from the given data.
Note: this task does not ask you to use transform-column
to clean the ART
Table
, but you can (and should) certainly try out what it looks like to use clean-kind
as an input to transform-column
for this purpose.
Learning Goals:
Task 9: Since the given ART
Table
has data errors and is pretty large (and therefore unwieldy to use in test cases), come up with two small example Tables
with the same columns as ART
. Make sure that the Table
s you create obey the criteria for what it means for the data to be clean, as given in Part 2. For the currency column, you can limit yourself to the following currencies (so that they match the CURRENCY-CONVERSION
Table
): CAD, CHF, CNY, EUR, JPY, MXN, USD, ZAR. You may end up revising these tables as you work through and test the following tasks – that's okay, but make the tables now as a starting point!
Task 10: (Submit a plan for this task!) Create a function called get-art-price
that takes in an art ID (Number
), a currency (String
), a Table
that has the same columns as CURRENCY-CONVERSION
and a Table
that has the same columns as ART
and returns a Number
for the price of the artwork with the given id, converted to the input currency. You can assume that exactly one row with the given id exist in the ART
-like input table. If a direct or inverse conversion doesn't exist (as described in Task 2), this function should raise("unable to convert price")
.
Task 11: (Submit a plan for this task!) You want to find the priciest art in the Table
, but you realize that this is hard to do when all of the artworks are in different currencies (for example, an artwork that costs 33,000 JPY is actually worth less than an artwork that costs 330 EUR, when they are converted to the same currency). You realize that you need to convert all of the artwork to the same currency in order to do this analysis. Create a function called priciest-art
that takes in a target currency (String
), a Table
that has the same columns as CURRENCY-CONVERSION
, and a Table
that has the same columns as ART
and returns the artwork ID (Number
) that costs the most once all artworks have been converted to the target currency. You can assume that all conversions to the given currency exist (either as direct or inverse conversions) in the input currency conversion table (*note: this means that you will have to examine CURRENCY-CONVERSION
to find such currencies when you are testing your function in order to obey this assumption).
Learning Goals:
There are a few different versions of databases of passengers who sailed on the Titanic. (There is some ambiguity in this term, because the Titanic made a few stops even on its maiden voyage.) One such database is in your starter code, called TITANIC-RAW
.
We're going to do an analysis of this dataset, using an assumption about names. We want to determine:
Relative to the dataset, we define the relevant concepts as follows:
We give you a helper function, first-name
, which takes in a full name as a String
and outputs the first name according to the criterion above.
Task 12: (Submit a plan for this task!) Using table functions and the first-name
helper function, determine the seven most popular male and female names according to our first name definition. You do not have to print them out directly in the interactions window – in fact, the easiest way to find these names will likely be to do some computations on the raw data and then find an appropriate function to summarize some data in the resulting tables (hint: where in the Table documentation is "summarizing" discussed?) Don't overcomplicate this – for reference, our solution uses 5 lines of code.
In your hw4-written.pdf
file:
Task 13: Does anything surprise you about your findings? Expand TITANIC-RAW
in your Pyret window and observe how names were recorded in the dataset. In your hw4-written.pdf
file, describe any observations you have made in this exploration. Before you started this task, did you believe that the definition of first name was reasonable (if no, briefly state why not). Did this change after your exploration?
Task 14: Read the following two short articles and include your answers to the questions below in hw4-written.pdf
Questions:
Hit "Run", then paste this check block into the interactions window. All the tests should pass: if they don't, this means you have a typo in a function header and the autograder won't run on your code!
hw4-code.arr
.hw4-written.pdf
. If your written file is not a PDF, we might not be able to grade it.hw4-code.arr
and hw4-written.pdf
on Gradescope.#thatPOWER by will.i.am ft. Justin Bieber
Brown University CSCI 0111 (Spring 2023)