473 views
   owned this note
# ARCHIVED: ~~Lab 7: Datatypes and Types of Data~~ :::danger ## This is the S26 version of this document. Refer to the [course webpage](https://brown-csci0111.github.io/pages/home.html) for the up-to-date version. ::: We've already seen a bunch of datatypes that have been built into Pyret, such as `String`, `Number`, `Table`, and `List`. We've even used our own custom datatypes, like `TimeData`! By creating custom datatypes, we can describe a piece of data with many components. Today, we'll do another design problem that draws on datatypes, tables, and lists. <details> <summary>What is <code>is-___</code>? (and why we prefer <code>cases</code>)</summary> InIn class, we saw the <code>cases</code> notation used to distinguish between variants. When you define a custom datatype, Pyret automatically creates an <code>is-___</code> function for each variant, that returns <code>True</code> if the input is of that dataclass' type and variant. For example, for the datatype ``` data Tech: | cellphone(type :: String) | laptop(brand :: String, memory-gigs :: Number) end ``` to tell if <code>my-tech-object</code> is a cellphone, we could call <code>is-cellphone(my-tech-object)</code>. <strong>In most situations (aka for all of the tasks we'll encounter in 111), however, we prefer that you use <code>cases</code>.</strong> This is because <code>cases</code> allows you to <i>simultaneously</i> ask which variant a piece of data is, and to assign names to the fields to use in any subsequent computations. <code>cases</code> also gets you in the habit of writing down a computation for every variant of a piece of data, which decreases hard-to-find bugs (for example, forgetting a variant when trying to use <code>is-___</code>). </details> ## Learning Objectives In this lab, the goal is to: * Think about ways of representing information * Practice communicating clearly about data design decisions * Practice defining datatypes * Use `cases` to deconstruct datatypes If you don’t feel more comfortable with datatypes after working on this lab, come to [TA hours](https://brown-csci0111.github.io/pages/calendar.html)! ## Resources [Lab 7 Presentation Slides](https://docs.google.com/presentation/d/1wXfJW2vNDGiP3qQj781yp_UXDNLs9LYRSSmBkR6YkeM/edit?usp=sharing) ## Part 1: Clean Up the Catalog You've been asked to write programs to help manage orders for items at a local store. The store maintains multiple tables: one a catalog of the items that they sell, and the other a table of the orders that have been placed on a given day (one table per day). We'll start with the catalog table. Import the [catalog Google Sheet](https://docs.google.com/spreadsheets/d/1PEh12EoH24gjsQKQhQY5WLIMiaKbs38MDFD29QJMRa0/edit?usp=sharing) into your program as a table by copying the following lines of code into the Pyret definitions window: # remove shared-gdrive statement if using context dcic2024 include shared-gdrive("dcic-2021", "1wyQZj_L0qqV9Ekgr9au6RX2iqt2Ga8Ep") include gdrive-sheets import lists as L import math as M ssid = "1REUzlRZ9aVZCO-KXb1d1vTpEnjJSvuFaRmghu7LX2GM" data-sheet = load-spreadsheet(ssid) item-table = load-table: item-id, item-description, price, mass, dimensions source: data-sheet.sheet-by-name("Sheet1", true) end **Task 1:** The first thing you've been asked to do is some analysis on the total volume (width x depth x height) of the items in the catalog. Look at the table you have loaded. Discuss with your partner how you might work with the data in the `"dimensions"` column to do this (you don't have to write the code, just discuss how you might do this). <br> Regardless of what your team decided, let's practice converting the dimensions strings into a collection of numbers that could be used to compute volume. **Task 2:** Create a `Dimension` datatype consisting of three numbers: width, depth, and height. **Task 3:** Use `transform-column` to replace each dimension string with a value of type `Dimension`. The original strings are formatted as: `"WIDTHxDEPTHxHEIGHT"`, e.g., `"12x10x5"`. *Hint: Look up `string-split-all` in the Strings documentation.* *Hint: Splitting the string still leaves you with the numbers in string form. You need to convert those to numbers. Since someone might try to convert a `String` that contains characters other than digits (e.g., `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 shop 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 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 | mass | dimensions |runtime | definition | | ---- | ----------- | ------ | ----- | -----|------------- |--------|------------| | Anne | Cork Board | 8.29 | 1 | 1.2 | 17x0.8x23 | 0 | 0 | | John | Fuzzy Socks | 2.50 | 5 | 0.1 | 9x7x0.5 | 0 | 0 | | Ken | Fedora | 16.99 | 2 | 0.1 | 9x9x4 | 0 | 0 | | Anne | Printer | 129.99 | 1 | 9.5 | 15.4x11.8x5.7| 0 | 0 | | Anne | Fuzzy Socks | 2.50 | 3 | 0.1 | 9x7x0.5 | 0 | 0 | | Ken | Printer | 129.99 | 1 | 9.5 | 15.4x11.8x5.7| 0 | 0 | | John | Lamp | 79.99 | 1 | 2.57 | 10.5x7x20.5 | 0 | 0 | | Ken | Fuzzy Socks | 2.50 | 1 | 0.1 | 9x7x0.5 | 0 | 0 | | Anne | Gift Card | 40.00 | 1 | 0 | 0 | 0 | 0 | | Ken | Gift Card | 20.00 | 2 | 0 | 0 | 0 | 0 | | Anne | Titanic | 15.00 | 1 | 0 | 0 | 195 | HD | | John | Oppenheimer | 17.00 | 1 | 0 | 0 | 180 | SD | | Ken | Zootopia | 12.00 | 1 | 0 | 0 | 108 | HD | Because they are both digital (i.e. not shipped by mail), movies and gift cards don't appear in the catalog. However, they are still for same and people can include them in their orders. *Note: many of values in the above table are 0, as not all columns apply to all items (e.g. a printer has no runtime). Keep this in mind for the subsequent tasks!* This part of lab asks you to think about the structure (column contents and types) of these tables and whether they can support the computations you'll need to do. These include: - 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. **Task 4:** Discuss with your partner the strengths and weaknesses of the current table organizations. Write down your main concerns as a collection of brief bullet points. **Task 5:** 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? **Task 6:** Now, take a look at two of our concrete proposals, listed in [this document](https://cs111-docs.cs.brown.edu/s/VbUBcHkXpc#). 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..."](https://www.youtube.com/watch?v=6FwmGLzyRDk) To get more practice working with datatypes, we will now work with the second of our proposals. **Task 7:** Define the datatypes `ItemData`, `Order`, and `UserOrder` as described in the document linked above. Remember to create a datatype to clearly show the two valid definitions, HD and SD. :::warning **NOTE**: Think about what variants this Datatype should have and what the respective fields should be. Are fields even necessary? ::: --- ### *(mini) CHECKPOINT:* Call over a TA once you reach this point to see your datatypes. --- **Task 8:** How specifically does this collection of datatypes address the issues that we identified with the original shop design? **Task 9:** Recreate the information of the above orders table with your new datatypes. :::warning **NOTE:** you should use all four of the datatypes you defined, in addition to the `Dimension` datatype from Task 2. ::: :::danger **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(...), order(...)]) ``` ::: **Task 10:** Write a function `more-socks` that takes a `List<Order>` (assume the list contains only physical items and not gift cards or movies) 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. **Task 11:** 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). Again, you can assume the list contains only physical items. **Task 12:** 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 per card. Physical items cost the price associated with the item (ignore tax, shipping costs, etc). Movies cost the price associated with the item, plus a $5 fee if they are in HD. Assume that the list contains the customer. *Hint: remember that you can use `cases` to determine the variant you're working with!* ___ ### *CHECKPOINT:* Call over a TA once you reach this point. ___ > Brown University CSCI 0111 (Spring 2026) > Have questions? Refer to [Ed](https://edstem.org/us/courses/93677) > Check out our glorious [Spotify](https://open.spotify.com/playlist/0UdtGEq1aamMp78T0Ehxg4) playlist! > <iframe data-testid="embed-iframe" style="border-radius:12px" src="https://open.spotify.com/embed/playlist/0UdtGEq1aamMp78T0Ehxg4?utm_source=generator" width="100%" height="352" frameBorder="0" allowfullscreen="" allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture" loading="lazy"></iframe> > > **Feedback form** > <iframe src="https://docs.google.com/forms/d/e/1FAIpQLSdjTJkyCYkLXcnYfzkugwD8FDuJQZwM5SAER4pwvU_QxTuRSw/viewform?embedded=true" width="640" height="977" frameborder="0" marginheight="0" marginwidth="0">Loading…</iframe>