Lesson 10: Traversals Investigate
45 minutes
Overview
In this lesson students work with partners to investigate three different apps that use traversal to access items in a lists. Students first explore all three apps without seeing the code to notice similarities and predict how they will work. Then they explore the code itself and make additions and modifications to the apps. To conclude the lesson, students review and discuss common programming patterns with traversals.
Purpose
After building a conceptual model using a for loop to traverse a list in the previous lesson, this lesson allows students to see how this is actually implemented in code. This lesson also introduces common programming patterns when using lists and traverals. Students will have some opportunities to modify working code in this lesson, but the most significant practice with lists and traversals will come in the following lesson.
Standards
AAP-3 - Programmers break down problems into smaller and more manageable pieces. By creating procedures and leveraging parameters, programmers generalize processes that can be reused. Procedures allow programmers to draw upon existing code that has already been tested, allowing programmers to write programs more quickly and with more confidence.
AAP-3.F - For simulations: a. Explain how computers can be used to represent real-world phenomena or outcomes. b. Compare the use of simulations with real-world contexts.
- AAP-3.F.1 - Simulations are abstractions of more complex objects or phenomena for a specific purpose.
- AAP-3.F.2 - A simulation is a representation that uses varying sets of values to reflect the changing state of a phenomenon.
- AAP-3.F.3 - Simulations often mimic real-world events with the purpose of drawing inferences, allowing investigation of a phenomenon without the constraints of the real world.
- AAP-3.F.4 - The process of developing an abstract simulation involves removing specific details or simplifying functionality.
- AAP-3.F.5 - Simulations can contain bias derived from the choices of real-world elements that were included or excluded.
- AAP-3.F.6 - Simulations are most useful when real-world events are impractical for experiments (e.g., too big, too small, too fast, too slow, too expensive, or too dangerous).
- AAP-3.F.7 - Simulations facilitate the formulation and refinement of hypotheses related to the objects or phenomena under consideration.
AP - Algorithms & Programming
- 3A-AP-14 - Use lists to simplify solutions, generalizing computational problems instead of repeated use of simple variables.
- 3B-AP-10 - Use and adapt classic algorithms to solve computational problems.
- 3B-AP-23 - Evaluate key qualities of a program through a process such as a code review.
Agenda
Objectives
Students will be able to:
- Explain the purpose of programming patterns with traversals both in terms of how they work and what they accomplish
- Identify common programming patterns using traversals
- Modify apps that make use of common programming patterns with traversals to adjust their functionality
Links
Heads Up! Please make a copy of any documents you plan to share with students.
For the teachers
For the students
- Data Tab in App Lab - Video (Download)
Teaching Guide
Lesson Modifications
Attention, teachers! If you are teaching virtually or in a socially-distanced classroom, please read the full lesson plan below, then click here to access the modifications.
Warm Up (5 minutes)
Discuss: Let's review the Traversal Machine. How did it work?
Discussion Goal: The Traversal Machine let us visualize how a for loop is used to traverse a list and interact with each element in the list one by one.
Activity (35 minutes)
Mile Tracker Investigation
Group: Group students into groups of four.
Level 1: Students read the entire code, and then students focuses their attention on a single function. Student should re-read the code for their function.
- Student A -
average()
(lines 32-38) - Student B -
slow()
(lines 40-48) - Student C -
fast()
(lines 50-58) - Student D -
numberedListDisplay()
(lines 60-66)
Discuss: How does the app work?
Students discuss in their groups how each of their functions work, specifically referencing the list it uses and how it is traversed using a for loop. Then as a class address any confusion over how the app works.
-
The goal: The goal of this investigation is to give students the opportunity to see lists traversed for different reasons and with different outcomes. Students should look at the code for their assigned function and make sense of what the function does overall and how it does it.
-
What to look for: It may be helpful to re-watch the video in the U5L10 - Traversals Explore lesson starting at 3:07 to hear how the speaker describes a section of code that processes a list for a specific purpose. Listening to that explanation will help give you an idea of what student explanations of their functions should sound like.
For example, when explaining the average function in this investigation, students should describe what the function does. In this case, the function calculates the average of the values stored in the
mileTimes
list and stores that value in theavg
variable. In addition to describing what the function does, students should describe how the function works. This will involve more detail.- Students should be able to describe that the function contains a local variable of
total
which is initialized at 0. - Then the function uses a for loop to traverse through the
mileTimes
list. In the loop, each value insidemileTimes
list is accessed and added to the current value stored inside the total variable. - When the loop reaches the end of the list, the loop ends and the total variable contains the sum of all the
mileTimes
. In the last line of the function, the value stored inavg
is updated by dividing the value stored intotal
by the total number of elements in themileTimes
list.
- Students should be able to describe that the function contains a local variable of
When you bring the class together to talk about how the app works, you should talk both about what the functions do as well as how they work. There likely is not time to go over all four functions, but discussing one or two of them will ensure the full class has an opportunity to hear how their peers describe code.
Prepping for Investigate Lessons: The best way to prepare for this lesson is to go through the experience yourself. Check out the three apps in Code Studio to get a sense for how they work. Then move on to the Code Investigation and actually try to answer all the questions for each app. These questions are intended to help students read and make sense of the code. It is not about getting the “right answer” as much as it is about building skills in your students to read, test, and make sense of code that they haven’t seen before. Finally, you will notice that for the levels where the directions ask students to “Modify” the code, you will find “Example Solutions” in the “Teacher Panel” of that level.
Data Tab Investigation
Level 2 (5 mins): Students read the code, and then investigate the data tab.
Discuss: Encourage students to share out what they found in the data tab, and how they can interact with it with code.
-
The goal: The goal of this investigation is to get students comfortable with information inside the “Data” tab and to help them understand the “getColumn” command. Students should be given some time to explore the Data tab themselves and even try the “Modify” task for a few minutes.
-
What to look for: When you bring the class together, students should be able to describe what information they see in the data tab and how they can use the data tab to help get access to data. Key points to draw out are:
- The “Data Library” on the left hand side has different categories of datasets that students can view.
- Students need to “import” a data set to be able to see the data inside it or to use the data in the code.
- Once imported, students will see the data set in the “Data Browser” area and can click on the name of the Data Set to see the data in table form.
- To use the
getColumn
command, you need the name of the table you want to use and the name of the column from that table. getColumn
will return the column from your table as a list. You can see it is a list because when it is printed to the console, it has square brackets[ ]
around the items which are separated by commas.
Modify: Students choose a new dataset to import to the project. Then students modify the code to print out a column of data from the chosen dataset. Point out to students that when they pull out a column, they are creating a list.
Do This: Show the Data Tab in App Lab video.
Note that in the next investigation, students will combine their knowledge of traversals and the “Data” Tab to make more robust apps that don’t require students to type long lists in their code themselves.
Random Dog Picker Investigation
Level 4: Instruct students to run the app and then carefully read it. There is a lot of information in the comments in this app. Students work through all of the questions on the screen.
Discuss: Go through the questions one by one and answer as a class. Afterwards, go line by line with the class explaining how the app works.
- The goal: The goal of this investigation is to give students an opportunity to make sense of how traversals and the datasets can be used together to create a working app. It is important to note that the algorithms in this app have some of the same qualities as the algorithm students will need to develop for the Create PT.
- What to look for: This project defines several different functions. Help guide students particularly to how the lists are filled. Students should see a
listSetup
function. This function calls thefilter
function.filter
is where the lists are being filled. Use the questions listed in CodeStudio to help students make sense of this code. In particular, spend a little extra time discussing the question “How are these lists filled?”. This is an opportunity to go through thefilter
function line by line to understand how the function works.
Traversals can be tricky! In this case, we are filtering a list by information that is in another list.
There are three lists to start: dogNames
, dogHeights
, and dogImages
. We want to sort out the dogs so that when the user picks a size from the dropdown, only dogs of that size show up in the display.
To do this, we traverse using a filter pattern. Using a loop to go through each item in the dogHeights
list, if a dog's height matches up to the requirements, we pull the dog's image and name from those original lists (all the indexes will match) and put those in the new filtered lists.
Once this is done, we have filtered lists that we can pull from to display a random dog's name and image.
Traversal Patterns
Levels 5-6: Review the patterns in these levels as a class.
- Have students add any relevant notes about the patterns to their journals.
- Discuss which patterns were used in the apps today.
Wrap Up (5 minutes)
Prompt: What aspects of using traversals to process a list do you feel you already understand? What questions do you want to dig into more tomorrow during the practice lesson?
Check For Understanding
Check For Understanding Question(s) and solutions can be found in each lesson on Code Studio. These questions can be used for an exit ticket.
Question: Explain how you would filter the dog dataset using traversal to have a filtered list of dogs who live long lives.
This work is available under a Creative Commons License (CC BY-NC-SA 4.0).
If you are interested in licensing Code.org materials for commercial purposes contact us.