Lists, Loops, and Traversals - Lesson 11: Traversals Practice

Overview

Students practice traversing lists, filtering and reducing lists, and using the data import tools. Along the way students develop debugging practices with traversals.

Goals

Students will be able to:

  • Write programs that use list traversals, including the filter and reduce patterns, with the support of sample code
  • Debug programs that use list traversals

Resources

Purpose

This lesson is students primary opportunity to get hands on with lists in code prior to the Make activity in the following lesson. Give students as much class time as you can to work through these. For this lesson it's recommended that you place students in pairs as a support and to encourage discussion about the challenges or concepts they're seeing.

Getting Started (2 minutes)

Remarks

  • Today we're going to have a chance to practice programming with a lot of the concepts and patterns we've explored over the last two lessons. I encourage you to go through these with a partner, but pay close attention to what each other is doing. In our next lesson you're going to have to use a lot of these on an independent project, and these activities are good practice for what you'll find there! Alright, let's get to it!
Teaching Tip

Move Quickly to the Activity: There's a lot in the main activity of today's lesson. You may optionally wish to do a quick vocabulary review or address any questions that came up in the last lesson. Otherwise, give students more time to get hands on with some code.

Activity (38 minutes)

Practice Time

Group: It is recommended that students work in pairs for this lesson and a number of the activities feature discussion prompts. Consider using pair programming, having drivers and navigators switch every 3 minutes, not every project.

Remarks

  • Today you're mostly going to practice what we've learned about programming with traversals. I'm here to help you when you need. However, I first want to remind you of the following:
    • Use your debugging skills. Try to zoom in on precisely where you're getting stuck.
    • Talk to your partner! That's what they're there for!
    • Hover over blocks to read the documentation about how they work.
    • Read the resources in the Help & Tips tab
    • Talk to the group next to you. If another group asks for help make sure to take some time to talk it through with them.
  • We can debug traversals by using many skills that helped us with lists, like using output statements. Another important new debugging skill will be to actually go look at your data in the original data file. Use output to make sure you're successfully getting the data you want.

Traversal Practice

Open a Project: Today's lesson has students investigate 7 different apps. Direct students to begin with Lesson11_App1.

Apps 1-3 Traversal Practice: These apps have students perform simple traversals over lists that are created inside their code (not by importing data from a file). In each program sample code is provided which students can use to help writing the code of their own.

  • App 1: traverse over a list and output every element
  • App 2: traverse over a list and output every element and its position
  • App 3: traverse over two parallel lists and print elements from both
Teaching Tip

Providing Support: Circulate around the room through the lesson encouraging students to use the strategies introduced at the beginning of the lesson. Students have a number of supports at their fingertips, so a big part of your role is helping build their independence in using those resources.

Reduce and Filter Practice

Apps 4-6 Reduce and Filter Practice: Students practice the reduce and filter patterns introduced in the previous lesson. In each program sample code is provided which students can use to help writing the code of their own.

  • App 4: filter a list of students to keep only those with more than 6 letters in their names.
  • App 5: reduce a list to find the maximum price inside of it.
  • App 6: filter a list of countries to find those in the Central America region. This program makes use of the data import tools.This program makes use of an imported data file.

App Practice

App 7 App Practice: In this level, students use the reduce pattern to finish building an app.

  • App 7: Write code for the "reduce" pattern to calculate a student's average grade
  • App 8: Write code to calculate and display the percent of each type of Air Quality Index day.

Wrap Up (5 minutes)

Synthesizing Discussion

Prompt: What aspects of working with traversals do you feel like clicked today? What do you still feel like you have trouble with?

Discussion Goal

Use this opportunity to address any lingering questions or misconceptions in the room. You can also use this as a source of discussion topics to kick off the following lesson. As you lead the discussion, call out the many resources students have access to help when they're getting stuck.

Discuss: Have students share with one another before sharing with the whole class.

Remarks

  • Working with traversals can be tricky. We will get more practice tomorrow by making an app that uses traversals and the data tools.

Assessment: Check for Understanding - AP Practice

For Students

Open a word doc or google doc and copy/paste the following question.

Question

What are the values stored in studentScores array after running this code snippet?

studentScores <- [77, 32, 45, 92, 86]

FOR EACH item IN studentScores
{
    IF (item > 60)
    {
        item <- item + 5
    }
    ELSE
    {
        item <- 0
    }
}

Standards Alignment

  • CSTA K-12 Computer Science Standards (2017): 3A-AP-14, 3B-AP-10
  • CSP2021: AAP-2.O.3

Next Tutorial

In the next tutorial, we will discuss Code.Org Unit 5 Lesson 12, which describes Make a project with traversals.