Day 13 - Operating on Lists
Skills: None
Pre-reading: 5.1.4 (aside from 5.1.4.1)
FIXME
Operating on Lists and Transforming Data
Focus: Work with built‑in operations on lists—computing statistics on numeric lists and using functions like filter, map, distinct, and getting elements by position.
1. Warm-Up & Recap (10 minutes)
- Recap:
- Quick review of what lists are from the previous lecture.
- Do Now:
- “Imagine you have a list of recipe ratings (numbers). What might you want to know? (e.g., highest rating, average rating)”
- Share ideas with the class.
2. Built‑In Operations on Numeric Lists (15 minutes)
- New Example:
Consider a table for recipe reviews:
reviews =
table: recipe, rating
row: "Pasta", 4.5
row: "Salad", 3.8
row: "Soup", 4.2
row: "Sandwich", 5.0
row: "Cake", 4.9
end - Extracting the Numeric Column:
ratings = reviews.get-column("rating")
- Computations:
- Use math/statistics libraries:
import math as M
import statistics as S
M.max(ratings) // highest rating
S.mean(ratings) // average rating
M.sum(ratings) // total of ratings
- Use math/statistics libraries:
- Interactive Exercise:
- Ask: “What is the median rating?”
- Have students compute using
S.median(ratings)
.
- Discussion:
- How could these statistics inform decisions (e.g., which recipe is most popular)?
3. Built‑In Operations on Lists of Strings (15 minutes)
- Discount Codes in a Food Delivery App:
Imagine a list of discount codes used:
discount-codes = [list: "NEWYEAR", "student", "NONE", "student", "VIP", "none"]
- Finding Unique Values:
- Use the lists library:
import lists as L
unique-codes = L.distinct(discount-codes) - Show the result (e.g.,
[list: "NEWYEAR", "student", "NONE", "VIP", "none"]
).
- Use the lists library:
- Filtering:
- Remove codes that represent no discount (in this case, normalized "none").
- Define a helper function:
fun is-real-code(code :: String) -> Boolean:
not(string-to-lower(code) == "none")
end
real-codes = L.filter(is-real-code, unique-codes) - Do Now:
- Ask: “What is the length of the resulting list?” (Hint: Use
L.length(real-codes)
.)
- Ask: “What is the length of the resulting list?” (Hint: Use
- Getting Elements by Position:
- Like how tables have
row-n()
, lists have a way of getting an element by position:first-code = real-codes.get(0)
- Discussion:
- Explain zero-based indexing and why
get(0)
returns the first element.
- Explain zero-based indexing and why
- Like how tables have
- Transforming Lists with Map:
- Extract the lowercase versions of each discount code:
lower-codes = L.map(lam(code): string-to-lower(code) end, codes-real)
- Ask students to predict the output.
L.map
is similar to howtransform-column
worked to update a column in a table, but since a list is just a single sequence of values, it updates the whole thing.
- Extract the lowercase versions of each discount code:
4. Wrap-Up & Reflection (10 minutes)
- Recap Key Operations:
- Filter, distinct, length, get, and map.
- Interactive Question:
- “How would you remove all ingredients that are completely lowercase from a list of ingredients?”
- (Set up for next lecture’s custom list functions.)