Pattern matching is a big part of computational thinking. It is part of generalising solutions. Here are some pattern matching puzzles suitable for those who are learning to code having moved on from block-based languages.
In maths you often have to complete sequences. Here we do a computer science equivalent. Not only do you need to work out the pattern, but then fit it into the template code. All the puzzle sequences generalise to the same computational pattern as expressed by the pseudocode.
While Loop Puzzles
Work out the pattern and complete the pseudocode below by replacing ?, ?? and ??? with different expressions (numbers, variables or calculations).
n = ? i = 0 while (n <= ??) begin print(n + “ “) n = n + ??? i = i + 1 end
For example, for the sequence 6 8 10 12…16
? is 6 ?? is 2 ??? is 16
Create a dry run / trace table and then write a program fragment in your favourite language to check your answer before looking at the solutions.
Puzzle Sheets
- Puzzle Sheet: Pseudocode Patterns Puzzle Sheet 1 [pdf] TO PRINT
- Puzzle Sheet: Pseudocode Patterns Puzzle Sheet 1 [pdf form] TO DO ON COMPUTER
- Solution Sheet: Pseudocode Patterns Puzzle Sheet 1 [pdf]