Brain DeCoded with CodeX - Mission 4, Objective 3: Pattern Detection
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.
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
🔤
Vocabulary
▾
🐍
New Python Code
▾
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
- 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.
-
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
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."
Students work through the reading on feature extraction and data representation.
- Students read the Gridlock section, where a grid of 0s and 1s stands in for the details of a shape.
- Define feature extraction: pulling specific details out of data and comparing them to a pattern already known.
- 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.
- Work the example grid and pattern in the reading as a class. Ask how many times the pattern appears.
grid[i][j]. Say it out loud a few times here and you will save yourself a dozen questions during work time.Students build pattern_detect.
- Students work through the CodeTrek steps, adding code to pattern_detect.
-
Students complete
detect(), comparing the four grid positions against the four positions of the pattern and settingfound = Trueon a match. - 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.
- Run the program. Press each button and watch which grid positions turn red.
- Students record their results on the Activity Guide, then change a pattern or add a grid as the guide directs.
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.