Pattern Recognition CS Thinking Example 2

Follow the full solution, then compare it with the other examples linked below.

Example 2

medium
Three programs are described: (a) calculates total price of items in a shopping cart, (b) calculates total marks of a student across subjects, (c) calculates total rainfall over a week. What pattern do they share?

Solution

  1. 1
    Step 1: All three take a list of numbers and add them up.
  2. 2
    Step 2: The pattern is: initialise a total to 0, iterate through a list, add each value to the total.
  3. 3
    Step 3: The same algorithm (summing a list) solves all three problems โ€” only the context differs.

Answer

All three use the 'sum a list of values' pattern. The same code structure works for all.
Recognising that different problems share the same underlying pattern lets us reuse solutions. This is a key skill in programming โ€” write once, apply many times.

About Pattern Recognition

Pattern recognition is the process of identifying similarities, trends, or regularities across data or problems in order to build general solutions. By spotting what is the same across different cases, you can create reusable strategies instead of solving each case from scratch.

Learn more about Pattern Recognition โ†’

More Pattern Recognition Examples