Brain DeCoded with CodeX - Mission 4, Objective 3: Pattern Detection

Mission 4 · Objective 3 Lesson Plan

Pattern Detection

Students widen the search from a line of letters to a grid, coding an algorithm that hunts a 2x2 pattern through rows and columns of 0s and 1s.

⏱ 45-70 min 🎯 Grades 8-12+ 💻 CodeSpace 🎮 CodeX 🔢 Grids & Matrices
View Lesson Outline
📋

Overview

Objective 2 searched one dimension, a word, for one simple thing, a repeated letter. This objective goes two-dimensional. Students build pattern_detect, which searches a grid of 0s and 1s for a 2x2 pattern the user picks with a button, then displays the grid on the CodeX with any match highlighted in red.

Along the way students meet the vocabulary that makes this work: feature extraction, pulling specific details out of data, and the matrix, a list of lists, which is how a computer holds a grid in the first place.

The contrast with a brain matters here. This algorithm walks the grid one position at a time, row by row and column by column. A brain, and a neural network, works in parallel across interconnected layers instead. Same job, very different method.

🎯 Project Goal: Students add code to a program that detects repeated patterns.

🎯

Learning Targets

  • I can explain feature extraction and what it is used for.
  • I can explain data representation for pattern detection in an algorithm.
  • I can explain data representation in a neural network.
💡

Key Concepts

  • Pattern recognition using an algorithm involves searching through rows and columns of data to match patterns.
  • Brains and neural networks use parallel processing and interconnected layers of neurons to recognize patterns.
✅

Assessment Opportunities

  • Turn in the Activity Guide.
  • Complete the program pattern_detect.
☑

Success Criteria

  • Complete the CodeTrek steps
  • Program runs correctly without errors
  • Activity Guide is completed
📂

Digital Resources

🧰

Classroom Materials

  • ▸CodeX device and USB cable, one per student
  • ▸Laptop/computer with Chrome browser
🚀

Extensions & Cross-Curricular

ExtensionThe Activity Guide asks students to change a pattern and then change or add a grid. Push them to add several grids in different sizes. Grids do not have to be square.
ExtensionFour patterns are used because there are four buttons. Have students change the code to add more patterns.
ChallengeAdvanced students can search for a pattern that is not 2x2, but that means rewriting the search algorithm itself. Challenge them to do it.
MathThis program expands a simple search into an advanced one that iterates over rows and columns. Have students find a simple math algorithm and expand it the same way, for example going from adding two single-digit numbers to adding two-digit numbers.
Lang ArtsPractice technical writing by composing a manual article explaining how the searching algorithm works.
🔤

Vocabulary

▾
Feature extraction:the process of looking for specific features in data.
Matrix:a list of lists, a two-dimensional (or more) grid of values.
Vector:a one-dimensional list of values.
🐍

New Python Code

▾
for i in range(len(grid)):
   for j in range(len(grid[i])):
Looping structure that traverses a grid. The outer loop steps through each row, the inner loop through each column.
📐

Standards

▾

Computer Science

9-12.DA.8
📝
Preparing for the Lesson
  • Run the program yourself first. Press each of the four buttons so you know what a found pattern and a missed pattern look like on the CodeX.
  • Decide how students get the Activity Guide. You can print a copy for each student or assign it digitally.
  • Have a grid and a 2x2 pattern drawn on the board before class. Students trace the search by hand much faster than they read it in code.
  • Expect typing errors. Plan a little extra work time and be ready to help students find a mismatched bracket or parenthesis.

🧑‍🏫
Teacher Notes
  • Typing in the code can be tricky. Sometimes parentheses () are used and sometimes square brackets []. Encourage students to read the CodeTrek slowly and carefully, and to pay extra attention as they type.
  • Students should follow the instructions on the Activity Guide and record their results.
  • The grid is chosen at random each run, so two students pressing the same button can get different results. That is expected, not a bug.
  • The search algorithm assumes a 2x2 pattern. Any pattern students add has to be 2x2 unless they also rewrite the search, which is the advanced challenge in the extensions.
  • Extensions and cross-curricular projects are included to enhance the concepts in the objective. You can use the extensions to extend students' learning.
🗺️

Lesson Outline

🗣️Warm-up / Hook

Do the search by hand before doing it in code.

  • Put a 4x4 grid of 0s and 1s on the board and a 2x2 pattern beside it. Ask students to find the pattern and say how many times it appears.
  • Ask: "What steps did you just follow? Say them in order, as if you were telling a computer."
Teaching tip: Students will spot the pattern all at once and then struggle to explain the steps. That gap is the point, and it sets up the parallel processing versus algorithm comparison at the end of the period.
🔢Reading & Discussion

Students work through the reading on feature extraction and data representation.

  1. Students read the Gridlock section, where a grid of 0s and 1s stands in for the details of a shape.
  2. Define feature extraction: pulling specific details out of data and comparing them to a pattern already known.
  3. Cover the Simplified Data Representation concept. A matrix is a list of lists, each row is a list of data points, and the values can be numbers, characters, or colors.
  4. Work the example grid and pattern in the reading as a class. Ask how many times the pattern appears.
Teaching tip: Row index first, then column, grid[i][j]. Say it out loud a few times here and you will save yourself a dozen questions during work time.
💻Student Work Time

Students build pattern_detect.

  1. Students work through the CodeTrek steps, adding code to pattern_detect.
  2. Students complete detect(), comparing the four grid positions against the four positions of the pattern and setting found = True on a match.
  3. Students wire up the main loop: show the instructions, read the button choice, select the pattern, pick a grid at random, run the detection, and display the results.
  4. Run the program. Press each button and watch which grid positions turn red.
  5. Students record their results on the Activity Guide, then change a pattern or add a grid as the guide directs.
Teaching tip: Most bugs here are brackets, not logic. When a student's program errors out, check the parentheses and square brackets on that line first before reading the algorithm.
✏️Wrap-up & Review

Close on algorithm versus neural network.

  • Ask: "Your program checked every position one at a time. When you found the pattern on the board, did you do that?"
  • Name the difference: the algorithm steps through rows and columns in order, while brains and neural networks process in parallel across connected layers.
  • Ask: "What would happen to your program if the grid were 1,000 by 1,000?"
  • Collect the Activity Guides.
Teaching tip: That last question is where students start to feel why neural networks exist. You do not need to resolve it here, just let the size problem sit with them.