Students continue practicing the Design Recipe, and explore scatter plots as a means of visualizing potential relationships between columns. They also learn how to build new columns, chain methods, and are introduced to Table Plans.
Students extend the animals table using functions they’ve defined earlier
Students define functions that sort, filter, or extend the animals table
Standards and Evidence Statements:
Standards with prefix BS are specific to Bootstrap; others are from the Common Core. Mouse over each standard to see its corresponding evidence statements. Our Standards Document shows which units cover each standard.
Data 3.1.1: Use computers to process information, find patterns, and test hypotheses about digitally processed information to gain insight and knowledge. [P4]
Computers are used in an iterative and interactive way when processing digital information to gain insight and knowledge.
Digital information can be filtered and cleaned by using computers to process information.
Combining data sources, clustering data, and data classification are part of the process of using computers to process information.
Insight and knowledge can be obtained from translating and transforming digitally represented information.
BS-DR.1: The student is able to translate a word problem into a Contract and Purpose Statement
given a word problem, identify the domain and range of a function
given a word problem, write a Purpose Statement (i.e. - rewrite the problem in their own words)
BS-DR.2: The student can derive test cases for a given contract and purpose statement
given a Contract and a Purpose Statement, write multiple examples or test cases
identifying correct and incorrect test cases for a function, based on its contract
BS-DR.4: The student can solve word problems that involve data structures
BS-PL.3: The student is able to use the syntax of the programming language to define values and functions
Length: 90 Minutes
Glossary:
scatter plot: A chart where points are placed on axes according to pairs of values
ReviewOpen your saved animals-dataset file. You should have several functions defined:
is-fixed
gender
is-cat
is-young
If you didn’t have a chance to type them in from your workbook, make sure you do!
Can you use the Design Recipe to write a few more functions?
Turn to Page 15, and see if you can solve the following word problems:
Define a function called nametag, which prints out each animal’s name in big red letters.
Define a function called birth-year, which consumes a Row of the animals-table and produces the year the kitten was born.
When your teacher has checked your work, add these two functions at the bottom of your Definitions Area, save your work, and click Run.
Building Columns
Overview
Learning Objectives
Students learn how to compute new columns in Pyret
Evidence Statementes
Product Outcomes
Students extend the animals table using functions they’ve defined earlier
Materials
Preparation
Building Columns(Time 5 minutes)
Building ColumnsSometimes we want to add a column to a Table, and we can use the .build-column method to do just that. The contract for this method is shown below, along with an example expression that adds a "label" column to the animals-table using your nametag function.
In the Interactions Area, use the .build-column method to produce a table that includes a born column, which contains the year each animal was born.
Chaining Methods
Overview
Learning Objectives
Students learn the syntax for chaining methods together
Evidence Statementes
Product Outcomes
Materials
Preparation
Chaining Methods(Time 20 minutes)
Chaining MethodsTable methods can be chained together, so that we can build, filter and order a Table. For example:
This code takes the animals-table, and builds a new column. According to our Contracts Page, .build-column produces a new Table, and that’s the Table whose .filter method we use. That method produces yet another Table, and we call that Table’s order-by method. The Table that comes back from that is our final result.
Suggestion: use different color markers to draw nested boxes around each part of the expression, showing where each Table came from.
It can be difficult to read code that has lots of method calls chained together, so we can break them up before each "." to make it more readable. Here’s the exact same code, written with each method on it’s own line:
How well do you know your table methods? Complete Page 16 in your Student Workbook to find out!
Have students discuss their answers.
Order matters.
Suppose we want to build a column and then use it to filter our table. If we use the methods in the wrong order (trying to filter by a column that doesn’t exist yet), we might wind up crashing the program. Even worse, the program might work, but produce results that are incorrect! When chaining methods, it’s important to build first, then filter, and then order.
So how do we know when we need which method, and in what order?
Table Plans
Overview
Learning Objectives
Students learn how to define functions using Table Plans
Evidence Statementes
Product Outcomes
Students define functions that sort, filter, or extend the animals table
Materials
Preparation
Table Plans(Time 20 minutes)
Table PlansSuppose you wanted to get a bar-chart of animal’s ages, for only the cats in the shelter, sorted alphabetically by name. How would you do that?
Then you want to get the same bar chart, but now for only the fixed animals, sorted alphabetically by name. How would you do that?
Then you want to get the same bar chart, but now for only the young animals, sorted alphabetically by name. How would you do that?
Have students volunteer this, whenever possible.
This is really repetitive, isn’t it? We’re always doing the same two things:
Define the table - in this case, by filtering and sorting.
Do something with it - in this case, make a bar chart.
Wouldn’t it be great if Pyret had a function that just took in a table of animals, and did these things for us? This seems like a problem we’ve seen before, back when we defined functions like gender or nametag: whenever we have the same code written over and over, we can use functions to save us the effort!
Rather than rewrite the same method chain for every table, we can define a function that consumes an entire table and does the work for us. That way we can write the method chain once, and re-use it over and over. We can modify the Design Recipe slightly to help us keep our method order straight, turning it into something called a Table Plan. Turn to page Page 17.
Table Plans are like the Design Recipe, but for manipulating tables. They enforce a way of thinking, which is important for your students.
Table Plans are a lot like the Design Recipe. They start with a Contract and Purpose Statement, but involve different kinds of examples and can often involve multiple function definitions. Let’s use our filtered bar chart as an example, which ties together all the pieces you’ve seen before.
Your students should be very comfortable with the Design Recipe before proceeding!
Step 1: Contract and Purpose
We’re going to build a function that does this for us, and we’ll start with the name. Naming is more complex in Table Plans, since we want to name the function according to the most important parts of what it does. Since we’re getting an alphabetically-sorted bar chart of their ages, we’ll call it sorted-age-bar. Instead of consuming Rows, this time we’re consuming and producing Tables. This gives us the following:
Ask students to volunteer other names - but push them to keep the relevant info as part of the name!
Step 2: Examples
This is really similar to writing examples with the Design Recipe, but everything stays on paper. First, we write down a small sample of the animals-table, called an Example Table. This example should contain all of the relevant columns, and enough rows to give us an idea for how the function should work. Then, on the righthand side, we write or sketch what we expect to get back.
What columns do we care about?
In the Interactions Area, how would you use this function, passing in the example-table?
What would sorted-age-bar(table) evaluate to?
In this case, we care about the name and age columns. According to our contract we would need to call the function’s name and pass in only a table. According to the purpose statement, typing sorted-age-bar(example-table) into the Interactions Area should give us a bar chart of all the animal’s ages, sorted by name.
Step 3: Define the Function in two parts
The final step is to define a function that executes our Table Plan. We’ll do this in two parts. First, we’ll define a table by applying our methods to the input. Then, we’ll do something with that table to produce a result. We already know how to start:
To define this table, we’ll ask ourselves three questions, in order:
Does our Result have more columns than our Sample Table? If so, we’ll need to use .build-column.
Does our Result have fewer rows than our Sample Table? If so, we’ll need to use .filter.
Does our Result have its rows in some order? If so, we’ll need to use .order-by.
If the answer to any of these questions is "no", cross out that line in the template.
Only .order-by is needed!
All three methods are needed, so we won’t cross anything out. You’re already familiar with definitions in Pyret, and that’s what we’ll use here. Let’s start with the name t for Table.
Are there more columns in our result? No, nothing new gets added.
Are there fewer rows in our result? No, the whole table get plotted.
Are the rows ordered? YES! According to our purpose statement, the animals are sorted alphabetically by name.
It may be helpful to start with the table definition all on one line, and have students see you break it up to accommodate chaining. Students should be reminded that both forms are valid, but encouraged to use the latter.
Now that we have our new table, t defined, what do we need to do with it? According to our purpose statement, we need to make a bar chart.
Drawing arrows from the t expression on the last line back to the t definition on the first line would be a good idea here. Make sure students see the connection between "defining the table...and using it"!
Once you’ve typed in the Contract, Purpose and Function Definition, click "Run". How do we use this function? If you look in the Examples section, you’ll see that the Result is written underneath the expression sorted-age-bar(animals-table). That’s the code that should give us the result, so let’s type it in!
Type in the code and hit Enter. Did you get back the same result you expected?
Looking for Relationships
Overview
Learning Objectives
Evidence Statementes
Product Outcomes
Materials
Preparation
Looking for Relationships(Time 15 minutes)
Looking for RelationshipsWhy are some animals adopted quickly, while others take a long time? What factors explain why one pet gets adopted right away, and others wait months?
Ask the class for theories.
One theory is that people adopt smaller animals because they’re easier to care for. How could we test that theory? Bar and pie charts are great for showing us how the values of a single column are distributed, but they can’t help us see connections between two columns.
Fortunately, Pyret lets us make many kinds of charts, including scatter plots! Here’s the contract for scatter-plot, as well as an example of a scatter plot that examines the relationship between weight and adoption time.
Try making a few scatter plots, looking for relationships between columns in the animals-table.
Another theory is that people adopt younger animals because they are cuter. But cats, dogs, rabbits and tarantulas have very different lifespans! A 5 year old tarantula is still really young, while a 5 year old rabbit is fully grown. With differences like this, it doesn’t make sense to put them all on the same chart! To do this analysis, we might have to make several charts, all of which do the same thing but operate on different tables: one for cats, one for dogs, etc.
Turn to Page 18 in your Student Workbook, and practice using a Table Plan write this function! Do you see a trend in the scatterplots for all the animals? For the cats? The young animals? The fixed animals? In which group is this trend the most clear?
Closing
Overview
Learning Objectives
Evidence Statementes
Product Outcomes
Materials
Preparation
Closing(Time 5 minutes)
Closing Building functions is a powerful technique, which you’ll use throughout the course. Today, you learned how to write functions that work on one row of a table at a time. In the next lesson, you’ll learn how to use those functions to loop over an entire table, letting us extend, filter, and sort our animals-table
Make sure to save your work. Hit the Save button in the top left. This will save your program in the code.pyret.org folder within your Google Drive.
If your students are working in pairs/groups, make sure that each student has access to a version of the program. The student who saved the program to their Google Drive can share their program with anyone by hitting the Publish button in the top left, choosing "Publish a new copy", then clicking the "Share Link" option. This will allow them to copy a link to the program, then send to their partners in an email/message.