Recognizing the Pattern in a Problem
Pattern recognition is spotting the shape of a problem from its input and its question. It helps a candidate pick a familiar approach quickly.
Signals in a problem
Signals are small facts about the input and the question.
signals = ["Input is in order", "Question asks for a pair", "Values repeat", "Input is short"]
for signal in signals:
print("Signal " + signal)A recognition checklist
A checklist turns recognition into three short questions.
questions = ["What is the shape of the input?", "What does the question ask for?", "What small input can test the guess?"]
for i in range(len(questions)):
print(str(i + 1) + " " + questions[i])Checking the shape
A short check shows whether a list is in order.
numbers = [1, 3, 5, 8]
print("In order " + str(numbers == sorted(numbers)))Example
problem_input = [2, 4, 6, 9]
print("Input " + str(problem_input))
print("In order " + str(problem_input == sorted(problem_input)))
print("Question pair that adds to 10")
print("First step test on a small input")Key points
- Pattern recognition starts with the shape of the input.
- It looks at what the question asks for.
- It looks for values that repeat or stay in order.
- It names the shape before any code is written.
- It tests the guess on one small input.
- A guess is a starting point and not a proof.
The program prints the sample input and checks its order. It states the question in plain words. It names testing on a small input as the first step.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
A problem gives a list in order and asks for a pair. What is the best first step?