How does your phone guess your next word?
You type "see you". Your phone suggests "soon". It is often right.
The phone has seen that people often write "soon" after "see you". It found a pattern. Programmers use the same skill to solve problems.
Key words
| Word | What it means | Example |
|---|---|---|
| Pattern | Something that repeats or looks alike. | 2, 4, 6, 8 |
| Pattern recognition | Finding patterns to help solve a problem. | two apps both need a login |
| Trend | A change that goes in one direction over time. | the bus is later each day |
| Similarity | A way that two things are alike. | both use a password |
| Prediction | A careful guess about what comes next. | Friday will be 6 minutes late |
| Assumption | Something you believe is true but have not checked. | there is no strike on Friday |
The cells students fill in are left blank in this preview.
Quick check: spot the pattern
Have a go
Quick check: spot the pattern
3 questionsWhy look for patterns?
Pattern recognition means finding what repeats or looks alike. Its purpose is to save time.
If a new problem looks like an old one, you can reuse the old solution. You do not start again.
Worked example: a trend
| Day | Minutes late | Change from the day before |
|---|---|---|
| Monday | 2 | none |
| Tuesday | 3 | up 1 |
| Wednesday | 4 | up 1 |
| Thursday | 5 | up 1 |
| Friday | ? | up 1, so 6 (a prediction) |
The cells students fill in are left blank in this preview.
Read the trend
Look at the last column. The bus is 1 minute later each day. That is a trend: a change in one direction.
You predict that Friday is 5 + 1 = 6 minutes late. A prediction is a careful guess, not a fact.
Make a prediction
Have a go
Make a prediction
3 questionsThree ways to use patterns
| Use | What you look for | Example |
|---|---|---|
| 1 Trends and similarities | Changes over time, and likeness inside one problem or between two problems. | Every enemy in a game moves left. A cinema booking is like a doctor booking. |
| 2 Existing solutions | A solved problem that looks like this one. | Reuse the login code from your last project. |
| 3 Predictions and assumptions | What will probably happen next, and what you accept as true. | Next month has the same sales. You assume no new shop opens. |
The cells students fill in are left blank in this preview.
Match the new problem to an old solution
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Watch: from a pattern to code
Press Play on the short film below. It finds the pattern in the bus table and turns the change that repeats into one step in code. Then it reuses that code for a new problem. Watch what happens when the pattern breaks.
Film: pattern recognition
Pattern recognition
Every scene, as text
- Pattern recognition means finding what repeats or looks alike. The bus is 2 minutes late on Monday, 3 on Tuesday, 4 on Wednesday and 5 on Thursday: up 1 each day. A change that goes in one direction over time is a trend.
- The change that repeats becomes one step in code, the lesson's Try it code. step = tuesday - monday works out the step, 1. friday = tuesday + step * 3 goes three steps on from Tuesday: Wednesday, Thursday and Friday. print(friday) prints 6, a prediction for Friday.
- Sales of 10, 20 and 30 pounds a day look like a new problem, but they go up by the same step each day, like the bus. So the old code is reused: only the numbers for monday and tuesday change, to 10 and 20, and the last three lines are the bus code unchanged. It prints 50 for Friday: Wednesday is 30, Thursday 40 and Friday 50. That is the purpose of pattern recognition: reuse what you already know, and save time.
- Myth (false): a pattern always carries on. A pattern carries on only while nothing changes. A bus strike on Friday breaks the trend, so the prediction of 6 minutes late is wrong.
- The cure: write down your assumption, and check it: is there a strike on Friday? The comment at the top says it: no strike on Friday. An assumption is something you believe is true but have not checked. The program still prints 6, a prediction: a careful guess, not a fact.
Try it: use the pattern
monday = 2
tuesday = 3
step = tuesday - monday
friday = tuesday + step * 3
print(friday)
# The step is 1. Tuesday is 3, so Wednesday 4, Thursday 5, Friday 6.
# Change tuesday to 5 and run it again.
Make a free account to save your work and track your progress.
Patterns can break
Warning
Many people think a pattern always carries on. It does only while nothing changes. A bus strike on Friday breaks the trend. Write down your assumption, and check it.
Your turn: use a pattern in code
Each function uses a pattern.
- next_number gives the next number when the step is the same each time. For 5 and 8 it gives 11.
- next_week_savings is the same problem with money. Reuse your idea from step 1.
- is_going_up says True if three numbers show a rising trend: each one is bigger than the one before.
Press Run Tests to check.
def next_number(first, second):
# The numbers go up by the same step each time.
# Give back the next number. For 5 and 8 the next is 11.
return 0
def next_week_savings(last_week, this_week):
# Savings grow by the same amount each week.
# Give back the money saved next week.
return 0
def is_going_up(first, second, third):
# Three numbers in time order, such as goals in three matches.
# Give back True if each number is bigger than the one before it.
return None
Make a free account to save your work, or press Start lab to work through the whole lesson.
Key words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Exit quiz
Have a go
Exit quiz
6 questionsRemember
Important
Find trends and similarities. Reuse old solutions. Make predictions, and write down your assumptions.