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.

Goals

Students will be able to:

Purpose

After building a conceptual model using a 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.

Getting Started (0 mins)

Prompt: Let's review last lesson. What did we do?

Discussion Goal

Last lesson, we visualized how a loop is used to traverse a list and interact with each element in the list one by one.


Activity (35 mins)

Group: Group students into groups of four.

Teaching Tip

Prepping for Investigate Lessons: The best way to prepare for this lesson is to go through the experience yourself. Check out the three apps 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.

Display Code at the Front: If your room allows it, display the code during the Code Investigation at the front of the room. When students mentions specific lines of code actually scroll to that line and read through it together.

Save Modifications for the End: This lesson can be tight on time. Rather than have students modify the code all at once, you can save modifications for the end of the Code Investigation and have students pick a single app they wish to modify.


Open a Project: Today's lesson uses three apps, starting with Lesson10_App1.

App 1: Students read the entire code, and then students focus their attention on a single action. Students should re-read the code for their function.

Discuss: Students discuss in their groups how each of their functions work, specifically referencing the list it uses and how it is traversed using a loop. Then as a class address any confusion over how the app works.

Modify: Students work with a partner to create a function that adds together the total time of every element in the list. Output the result. Call this function in the UpdateScreen() function.


App 2: Students read the code, and then investigate the data folder.

Discuss: Encourage students to share out what they found in the datasets and how they can interact with it with code.

Modify: Students load a new dataset into the project. Then students modify the code to print out a column of data from the new dataset. Point out to students that when they pull out a column, they are creating a list.


App 3: 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.

Discussion Goal

Make sure students arrive at the following answers:


Teaching Tip

Traversals can be tricky! In this case, we are filtering a list by information that is in another list.

There are two lists to start: dogNames and dogHeights. We want to sort out the dogs so that when the user picks a size via the buttons, 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 name from the original list (all the indexes will match) and put that in the new filtered list.

Once this is done, we have a filtered list we can pull from to display a random dog's name and image.


Pattern Review: Review the two patterns below as a class: the List Filter Pattern and the List Reduce Pattern.

List Filter Pattern

Filtering one list

This pattern allows the user to filter a list into a new list by limiting the information that is added to the new list. For example, you could filter a list of words so that only words of a certain length or words that start with a specific letter appear in the new list.

Example: List Filter - starting letter

Array<text> list
list:Add("apples")
list:Add("oranges")
list:Add("apricots")
list:Add("bananas")

Array<text> filteredList

integer i = 0
repeat while i < list:GetSize()
    text firstLetter = list:Get(i):GetSubtext(0, 1)
    if firstLetter = "a"
        filteredList:Add(list:Get(i))
    end
    i = i + 1
end

Example: List Filter - length

Array<text> list
list:Add("apples")
list:Add("oranges")
list:Add("apricots")
list:Add("bananas")

Array<text> filteredList

integer i = 0
repeat while i < list:GetSize()
    integer wordLength = list:Get(i):GetSize()
    if wordLength > 7
        filteredList:Add(list:Get(i))
    end
    i = i + 1
end

How does it work?

Filtering multiple lists

What if you have multiple lists, but want to filter all the lists based on the information in a single list?

Example: Multiple List Filter

Array<text> studentNameList
Array<integer> studentGradeList
Array<integer> studentAgeList

Array<text> filteredStudentNameList
Array<integer> filteredStudentGradeList
Array<integer> filteredStudentAgeList

action Start
    studentNameList:Add("Sal")
    studentNameList:Add("Maya")
    studentNameList:Add("Rudy")
    studentNameList:Add("Gina")
    studentNameList:Add("Paris")
    
    studentGradeList:Add(10)
    studentGradeList:Add(11)
    studentGradeList:Add(10)
    studentGradeList:Add(12)
    studentGradeList:Add(11)
    
    studentAgeList:Add(16)
    studentAgeList:Add(18)
    studentAgeList:Add(15)
    studentAgeList:Add(17)
    studentAgeList:Add(17)

    Filter()
end

action Filter
    // Start with blank lists
    filteredStudentNameList:Empty()
    filteredStudentGradeList:Empty()
    filteredStudentAgeList:Empty()

    integer i = 0
    repeat while i < studentGradeList:GetSize()
        if studentGradeList:Get(i) = 11
            filteredStudentNameList:Add(studentNameList:Get(i))
            filteredStudentGradeList:Add(studentGradeList:Get(i))
            filteredStudentAgeList:Add(studentAgeList:Get(i))
        end
        i = i + 1
    end
end

All the lists must be the same length for this pattern - ideally they are columns pulled from a table

How does it work?

List Reduce Pattern

This pattern is used when you want to reduce the information in a list down to a single number. For example, you might want to find the average of a list of numbers or the smallest number.

Example: List Reduce - average

Array<number> list
number average = 0

action Start
    list:Add(1)
    list:Add(2)
    list:Add(3)
    list:Add(4)
    list:Add(5)

    FindAverage()
end

action FindAverage
    number total = 0
    integer i = 0
    repeat while i < list:GetSize()
        total = total + list:Get(i)
        i = i + 1
    end
    average = total / list:GetSize()
end

How it works

Example: List Reduce - smallest

Array<number> list
number smallest = 0

action Start
    list:Add(5)
    list:Add(4)
    list:Add(3)
    list:Add(2)
    list:Add(1)

    FindSmallest()
end

action FindSmallest
    number temp = list:Get(0)
    integer i = 0
    repeat while i < list:GetSize()
        if list:Get(i) < temp
            temp = list:Get(i)
        end
        i = i + 1
    end
    smallest = temp
end

How it works

Wrap Up (5 mins)

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?

Assessment: Check for Understanding

Question: Explain how you would filter the dog dataset using traversal to have a filtered list of dogs who live long lives.

Standards Alignment