Appearance
A2 · Pattern Recognition
Spec reference: Section A - Computational Thinking
Key idea: Identify similarities, trends and repeated structures within problems.
Pattern Recognition
Identifying common elements or features
Patterns appear in two ways in computing:
1. Patterns within a single problem
When you look at one problem closely, you often notice the same operation happening repeatedly in different places.
Example: Drawing multiple shapes
If a program needs to draw a square, a triangle, and a pentagon, you notice a pattern:
- Each shape is made of straight lines.
- Each line connects two points.
- Each shape is closed (the last point connects back to the first).
The pattern → you can write one function that draws any polygon rather than separate code for each shape.
2. Patterns across different problems
When you have solved several problems, you recognise that a new problem is similar to one you have already solved.
Example: Login vs booking systems
| Login system | Booking system |
|---|---|
| Get username & password | Get customer ID & date |
| Validate against database | Check availability in database |
| Allow or deny access | Confirm or decline booking |
The pattern → both systems: take input → check database → respond with success or failure. The same structural solution applies to both.
Why pattern recognition matters
Recognising patterns lets programmers:
| Benefit | Explanation |
|---|---|
| Reuse code | Write a function once, call it many times |
| Reduce errors | Fewer lines of unique code means fewer bugs |
| Save time | Adapting an existing solution is faster than starting from scratch |
| Create algorithms | Patterns in data often reveal the algorithm needed to process it |
Pattern recognition in data
Sometimes the pattern is in the data itself, not the code structure.
Example: Temperature readings
Mon: 14°C Tue: 13°C Wed: 15°C Thu: 14°C Fri: 12°CPattern → temperatures vary within a small range. A simple averaging algorithm is appropriate. You don't need a complex model.
Example: Exam scores
Student A: 45, 47, 46, 44, 45
Student B: 20, 65, 91, 12, 88Pattern → Student A's scores are consistent (low variance), Student B's are erratic (high variance). Different reporting logic may be needed for each.
Pattern recognition and functions
The most direct practical use of pattern recognition in programming is spotting when to write a function. If you find yourself writing the same block of code more than once, that is a pattern - and you should extract it into a reusable function.
python
# WITHOUT pattern recognition - repeated code
print("Hello, Alice")
print("Hello, Bob")
print("Hello, Carol")
# WITH pattern recognition - function captures the pattern
def greet(name):
print("Hello, " + name)
greet("Alice")
greet("Bob")
greet("Carol")Exam tip
In the exam, if you are asked to identify patterns in a scenario, look for:
- Steps that repeat with different data.
- Processes that are structurally identical to a previous example.
- Data values that follow a trend or cycle.
State the pattern explicitly: "The same validation process is used for both the username and the postcode fields - in both cases the input is checked against a regular expression pattern."
Summary
| Concept | Meaning |
|---|---|
| Pattern recognition | Finding repeated elements or similarities in problems |
| Common features | Shared characteristics across different problems |
| Code reuse | Using the same solution (e.g. a function) in multiple places |
| Data patterns | Trends or regularities in data values |
Test Yourself
Question 1 of 5
What is pattern recognition in the context of computational thinking?