Lab 7: Data Types. Types of Data.
If you are completing this lab on your own:
To get full points for this lab:
- Submit your code file, labeled
lab7.arr
, on Gradescope by 6pm Friday
- Put your name at the very top of the file in a comment
- Your code submission must run
- Leave comments where we ask you to discuss/think about something
- You must have either:
- completed the lab
- OR shown 2 hours worth of effort and left a block comment at the bottom of your file describing what you tried for the parts you didn’t finish
Data? Types?
We’ve already seen a bunch of data types that have been built into Pyret, such as String
, Number
, Table
, and Boolean
. We’ve even used our own custom data types, like TimeData
and Posn
! By creating custom data types, we can describe a piece of data with many components.
Today, we’ll do another design problem that draws on datatypes, tables, and lists.
For this case, we’re asking you to go undercover and join a rag-tag band of rascals. In order to blend in, you’ll need to buy a disguise.
You head to your favorite clothes shoppe, which provides its catalog as an unsophisticated spreadsheet. You start sending in orders by carrier pigeon (they weren’t extinct yet), but the shoppe’s pigeons take a long time to come and have strange totals. You persuade the owners of the shoppe to give you permission to take a closer look at the data organization being used under the hood.
The first thing you notice is that the catalog spreadsheet gets loaded into a table, but the dimensions are stored as strings rather than in a format that makes it easy to determine item shapes and volumes for shipping purposes. You set out to fix this problem.
-
Import the catalog google sheet into your program as a table by copying the following lines of code into the Pyret definitions window:
include shared-gdrive("cs111-2019.arr", "1anLZcL_Qr11cdmD2KO8UomSGb6pfOHV8")
include gdrive-sheets
import lists as L
import math as M
ssid = "1pNe1SQXe1IGOgI3XyAU4ugk9Ko5T3gzebJZZK-LZ7MM"
data-sheet = load-spreadsheet(ssid)
item-table =
load-table: item-id, item-description, price, weight, dimensions
source: data-sheet.sheet-by-name("Sheet1", true)
end
-
Create a Dimension
datatype, and transform each String in the “dimensions” column of the loaded catalog table into a Dimension
(use transform-column
).
NOTE: the original dimensions
column is formatted as: "WIDTHxDEPTHxHEIGHT"
You’ll want to use the function string-to-number
to convert the original dimensions from Strings to Numbers. Since someone might try to convert a String that contains characters other than digits (ex: string-to-number("!@#")
), string-to-number
returns a special type (called an Option
) that signals whether the conversion worked. Here’s how to use string-to-number
to extract the Number for a String of only digits (note the .value
on the end):
>>> string-to-number("!@#")
none
>>> string-to-number("42")
some(42)
>>> string-to-number("42").value
42
CHECKPOINT: Call over a TA once you reach this point.
Part 2: Online Ordering
Next, you get a peek at how the shoppe is storing its orders. Each day, one table is created. That table contains a “name” column, specifying the person who placed the order, and a “description” column, listing the item that the corresponding person bought. Orders are bundled together and shipped on horseback each evening, so one shipment goes to each person whose name appears in the table (for instance, in the table below, John won’t receive two separate orders). Here is an example:
name |
description |
price |
count |
weight |
dimensions |
Anne |
Cork Board |
8.29 |
1 |
1.2 |
17x0.8x23 |
John |
Fuzzy Socks |
2.50 |
5 |
0.1 |
9x7x0.5 |
Ken |
Fedora |
16.99 |
2 |
0.1 |
9x9x4 |
Anne |
Printer |
129.99 |
1 |
9.5 |
15.4x11.8x5.7 |
Anne |
Fuzzy Socks |
2.50 |
3 |
0.1 |
9x7x0.5 |
Ken |
Printer |
129.99 |
1 |
9.5 |
15.4x11.8x5.7 |
John |
Lamp |
79.99 |
1 |
2.57 |
10.5x7x20.5 |
Ken |
Fuzzy Socks |
2.50 |
1 |
0.1 |
9x7x0.5 |
Anne |
Gift Card |
40.00 |
1 |
0 |
0 |
Ken |
Gift Card |
20.00 |
2 |
0 |
0 |
Gift cards (a relatively new technology) don’t appear in the catalog because they are by word-of-mouth, but people can still include them in their orders.
However, being a methodical and careful sheriff, you are quite unsettled by this method of tracking orders. They know that the shoppe needs to be able to perform several tasks with its data:
- Compute the total cost of each person’s order.
- Compute the total volume (with respect to spatial dimensions) of each person’s order.
- Compute the maximum single dimension of each person’s order (determines box size for shipping).
- Compute their total sales per day.
- Compute how many of each item gets sold for purposes of inventory.
Look at the two tables (catalog and orders), and keep these tasks in mind as you work on the following problems:
-
Discuss with your partner the strengths and weaknesses of the current data organization. Write down your main concerns as a collection of brief bullet points.
-
Spend 5-10 minutes coming up with an alternative proposal for the datatypes, tables, and lists that you might use. Indicate the types of all of your columns, components, and list contents. How does your proposal address each of your concerns from the previous question?
-
Now, take a look at two of our concrete proposals, listed in this document. Which do you prefer and why?
CHECKPOINT: Call over a TA once you reach this point.
Part 3: The Tables Have Turned
“Well, well, well, how the turntables…”
To get more practice working with datatypes, we will now work with the second of our proposals.
-
Define the datatypes for each of Order
, UserOrder
, and ItemData
as described in that proposal.
-
How specifically does this collection of datatypes address the issues that we identified with the original shoppe design?
-
Recreate the information in the above example orders table with your new datatypes (name it orders
).
NOTE: you should use all three of the datatypes you defined, in addition to the dimension datatype.
NOTE: you do not need to create a new table using these datatypes. We want you to rewrite the data represented in the table with your datatypes. For example, to represent John’s orders, we can write something like
john = user-order("John", [list: order(fuzzy-socks, 5), order(lamp, 1)])
-
Write a function any-oversized
that takes a List<Order>
and returns a Boolean indicating whether any single item in the order has a total linear dimension (length + width + height) of more than 40 inches (the same units as the original table).
-
Write a function more-socks
that takes a List<Order>
(that does not contain any gift cards) and returns a List<Order>
that has the items from the original orders, except each item matching the description “Fuzzy Socks” has its count increased by 1.
-
Write a function order-cost
that takes a List<UserOrder>
and a customer name and returns the total price of the items in that person’s orders. Gift cards cost their amount plus a 50-cent processing fee. Physical items cost the price associated with the item (ignore tax, shipping costs, etc). Assume that the list contains the customer.
CHECKPOINT: Call over a TA once you reach this point.
Crooks caught!
As usual, you’ve solved the case in record time. The undercover operation was a success and all the crooks have been caught thanks to your quick coding.
Lab 7: Data Types. Types of Data.
If you are completing this lab on your own:
To get full points for this lab:
lab7.arr
, on Gradescope by 6pm FridayData? Types?
We’ve already seen a bunch of data types that have been built into Pyret, such as
String
,Number
,Table
, andBoolean
. We’ve even used our own custom data types, likeTimeData
andPosn
! By creating custom data types, we can describe a piece of data with many components.Today, we’ll do another design problem that draws on datatypes, tables, and lists.
Part 1: Shopping Spree
For this case, we’re asking you to go undercover and join a rag-tag band of rascals. In order to blend in, you’ll need to buy a disguise.
You head to your favorite clothes shoppe, which provides its catalog as an unsophisticated spreadsheet. You start sending in orders by carrier pigeon (they weren’t extinct yet), but the shoppe’s pigeons take a long time to come and have strange totals. You persuade the owners of the shoppe to give you permission to take a closer look at the data organization being used under the hood.
The first thing you notice is that the catalog spreadsheet gets loaded into a table, but the dimensions are stored as strings rather than in a format that makes it easy to determine item shapes and volumes for shipping purposes. You set out to fix this problem.
Import the catalog google sheet into your program as a table by copying the following lines of code into the Pyret definitions window:
Create a
Dimension
datatype, and transform each String in the “dimensions” column of the loaded catalog table into aDimension
(usetransform-column
).NOTE: the original
dimensions
column is formatted as:"WIDTHxDEPTHxHEIGHT"
You’ll want to use the function
string-to-number
to convert the original dimensions from Strings to Numbers. Since someone might try to convert a String that contains characters other than digits (ex:string-to-number("!@#")
),string-to-number
returns a special type (called anOption
) that signals whether the conversion worked. Here’s how to usestring-to-number
to extract the Number for a String of only digits (note the.value
on the end):CHECKPOINT: Call over a TA once you reach this point.
Part 2: Online Ordering
Next, you get a peek at how the shoppe is storing its orders. Each day, one table is created. That table contains a “name” column, specifying the person who placed the order, and a “description” column, listing the item that the corresponding person bought. Orders are bundled together and shipped on horseback each evening, so one shipment goes to each person whose name appears in the table (for instance, in the table below, John won’t receive two separate orders). Here is an example:
Gift cards (a relatively new technology) don’t appear in the catalog because they are by word-of-mouth, but people can still include them in their orders.
However, being a methodical and careful sheriff, you are quite unsettled by this method of tracking orders. They know that the shoppe needs to be able to perform several tasks with its data:
Look at the two tables (catalog and orders), and keep these tasks in mind as you work on the following problems:
Discuss with your partner the strengths and weaknesses of the current data organization. Write down your main concerns as a collection of brief bullet points.
Spend 5-10 minutes coming up with an alternative proposal for the datatypes, tables, and lists that you might use. Indicate the types of all of your columns, components, and list contents. How does your proposal address each of your concerns from the previous question?
Now, take a look at two of our concrete proposals, listed in this document. Which do you prefer and why?
CHECKPOINT: Call over a TA once you reach this point.
Part 3: The Tables Have Turned
“Well, well, well, how the turntables…”
To get more practice working with datatypes, we will now work with the second of our proposals.
Define the datatypes for each of
Order
,UserOrder
, andItemData
as described in that proposal.How specifically does this collection of datatypes address the issues that we identified with the original shoppe design?
Recreate the information in the above example orders table with your new datatypes (name it
orders
).NOTE: you should use all three of the datatypes you defined, in addition to the dimension datatype.
NOTE: you do not need to create a new table using these datatypes. We want you to rewrite the data represented in the table with your datatypes. For example, to represent John’s orders, we can write something like
Write a function
any-oversized
that takes aList<Order>
and returns a Boolean indicating whether any single item in the order has a total linear dimension (length + width + height) of more than 40 inches (the same units as the original table).Write a function
more-socks
that takes aList<Order>
(that does not contain any gift cards) and returns aList<Order>
that has the items from the original orders, except each item matching the description “Fuzzy Socks” has its count increased by 1.Write a function
order-cost
that takes aList<UserOrder>
and a customer name and returns the total price of the items in that person’s orders. Gift cards cost their amount plus a 50-cent processing fee. Physical items cost the price associated with the item (ignore tax, shipping costs, etc). Assume that the list contains the customer.CHECKPOINT: Call over a TA once you reach this point.
Crooks caught!
As usual, you’ve solved the case in record time. The undercover operation was a success and all the crooks have been caught thanks to your quick coding.