All subjects · Computer Science · WCP02 Practical Programming and Problem-solving

Computational thinking in code: abstraction, decomposition, patterns and generalisation

Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving

The four parts of computational thinking each show up in the code you write. The specification ties each one to a programming habit.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Abstraction (spec 10.1.1)

Abstraction hides detail that is not needed. In code, a subprogram hides its implementation: whoever calls is_prime(n) needs to know what it returns, not how it works. The stack lesson's push() and pop() are abstractions: the main program never touches top directly.

Decomposition (spec 10.1.2)

Break the problem into parts and describe the algorithm in a first comment before coding, then turn each part into code or a subprogram:

# Read sensor readings from the file
# Find the first invalid reading for each sensor
# Display the faulty sensors in a table and write them to a file

Pattern recognition and generalisation (spec 10.1.3, 10.1.4)

Pattern recognition: spot that a new problem is like one you have solved and reuse the same solution, subprogram or data structure (a running total, a linear search, a 2D list of records).

Generalisation: write one solution for a family of problems by turning fixed values into parameters.

def count_in_range(values, low, high):
    count = 0
    for v in values:
        if low <= v <= high:
            count = count + 1
    return count

marks = [35, 72, 58, 90, 41]
print(count_in_range(marks, 70, 100))
print(count_in_range(marks, 40, 59))
ages = [12, 17, 15, 19]
print(count_in_range(ages, 13, 19))

Output:

2
2
3

Worked example

A program has three loops: one counts marks of 70 or more, one counts marks from 50 to 69, one counts marks below 50. Generalise them.

  1. Recognise the pattern: each loop counts values between two limits.
  2. Write one function count_in_range(values, low, high).
  3. Call it three times: (70, 100), (50, 69), (0, 49).

Answer: One general function with the limits as parameters replaces three loops

Key idea: Abstraction: subprograms hide how; decomposition: plan the parts in a first comment; pattern recognition: reuse known solutions; generalisation: turn fixed values into parameters.

Check yourself

  1. Which is the best example of abstraction in a program?
    • Writing every step of the program in the main block
    • Calling is_valid(mark) without needing to know how it checks the mark
    • Using a for loop instead of a while loop
    • Printing the value of every variable
    Show answer

    Calling is_valid(mark) without needing to know how it checks the mark. The subprogram hides its implementation behind a name and an interface.

  2. A function draws a 5 by 5 grid of stars. How would you generalise it?
    • Copy it and change 5 to 6 in the copy
    • Add a comment saying it draws a grid
    • Make the grid size a global variable that other code changes
    • Add parameters for the number of rows and columns
    Show answer

    Add parameters for the number of rows and columns. Generalisation turns the fixed values into parameters so one function solves the whole family of problems.

  3. According to the specification, how can you show decomposition in your code?
    • Write the whole program on one line
    • Begin with a first comment that describes the steps of the algorithm
    • Use only global variables
    • Avoid subprograms
    Show answer

    Begin with a first comment that describes the steps of the algorithm. A first comment outlining the parts is the decomposition, and each part then becomes code.

  4. You have written a linear search for names in a list. A new task needs to find a product code in a list of codes. What does pattern recognition suggest?
    • Write a completely new algorithm
    • Sort the list three times first
    • Reuse the same linear search approach or subprogram
    • Store the codes in a text file instead
    Show answer

    Reuse the same linear search approach or subprogram. The problem has the same shape, so the same solution applies.

  5. What is displayed when this code runs? ```python def count_in_range(values, low, high): count = 0 for v in values: if low <= v <= high: count = count + 1 return count print(count_in_range([4, 9, 15, 21, 30], 10, 25)) ```
    Show answer

    2. Values from 10 to 25 inclusive are 15 and 21: 2.

  6. What is displayed when this code runs? ```python def repeat_char(ch, n): line = "" for i in range(n): line = line + ch return line print(repeat_char("#", 3) + repeat_char("-", 2)) ```
    Show answer

    ###--. The general function builds ### and --, joined: ###--.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.