Keep: details needed for this decision
- Book identifier: selects the requested book.
- Available-copy count: tells us whether a copy can be reserved.
Loading…
Free A-Level Computer Science guide
Learn two ways to make a complex problem manageable. Choose the details a solution needs, then divide its work into named subproblems. Suitable for AS and A-Level Computer Science.
Content owner: Michael Print · Written for A-Level learners · Checked against official specifications
Abstraction creates a simplified model by keeping the details relevant to its purpose and leaving out unnecessary detail. Decomposition divides a problem into smaller subproblems that can be solved and tested separately.
For a library availability check, abstraction helps us represent a book using its identifier and available-copy count. Decomposition helps us separate checking availability, updating that count and reporting the result.
You can follow the model without knowing Python. To trace the code, you should recognise variables, an if statement and a function that returns a value. The lists and functions guide explains the function syntax.
A school library wants to check whether a requested book has a copy available, reserve one copy if it does, and report the result. Our original example focuses on that stock decision and uses invented book identifiers.
The model is a simplified view of the real library. Its purpose determines what is relevant: a different task, such as helping someone find a book, would need its shelf location. Abstraction does not mean removing every detail you can.
Worked example
The availability result controls whether the update runs. Naming these subproblems lets us investigate an incorrect stock count separately from an incorrect message. Decomposition is useful even before any code is written.
This complete Python example stores the simplified stock model in a dictionary: each book identifier maps to a count. has_available_copy() solves the checking subproblem; reserve_copy() uses that result to update the count and return a message.
def has_available_copy(book_id, copies):
return copies.get(book_id, 0) > 0
def reserve_copy(book_id, copies):
if not has_available_copy(book_id, copies):
return "Unavailable"
copies[book_id] -= 1
return "Reserved"
copies = {"BK17": 2, "BK42": 0}
print(reserve_copy("BK17", copies)) # Reserved
print(copies["BK17"]) # 1
print(reserve_copy("BK42", copies)) # Unavailablecopies.get(book_id, 0) returns the stored count, or zero if the identifier is absent. return immediately ends the current function call. The dictionary is shared between the two functions, so decreasing a count changes the stock seen by later calls.
BK17 starts with two available copies. The check returns True, the count becomes one, and the first line prints Reserved.1.BK42 has zero copies. The check returns False, so the function returns Unavailable without changing its count.These three original practice questions are worth 11 marks in total. The answers give indicative marking guidance, not an examination board's mark scheme.
A cinema model checks whether a requested showing has a seat available. Possible details are its showing identifier, number of remaining seats, poster colour and building height. Identify two details the model needs. Name one detail it can omit and explain why.
Indicative answer: Keep the showing identifier (1) and number of remaining seats (1). Omit poster colour (1), because the model decides whether a seat is available and the poster's appearance does not affect that decision (1). Accept building height as the omitted detail with a relevant justification.
A cinema booking system receives a showing identifier, reserves one seat if any remain, and reports the booking result. Name two distinct subproblems that this task could be divided into. Explain one benefit of this decomposition when developing or testing the solution.
Indicative answer: Check whether the selected showing has a remaining seat (1); update its remaining-seat count after a successful booking (1). The availability check can be tested separately, helping locate a fault before investigating the count-update step (1). Award one mark per distinct relevant subproblem, up to two; accept receiving the showing identifier or reporting the booking result. The benefit must be linked to a named subproblem; “it is easier” alone earns no mark.
Keep the two functions from the worked example, but replace the main program with the code below. State its three output lines in order. Explain how has_available_copy() demonstrates decomposition.
copies = {"BK08": 2, "BK63": 1}
print(reserve_copy("BK63", copies))
print(copies["BK63"])
print(reserve_copy("BK63", copies))Indicative answer: The three output lines are Reserved, 0 and Unavailable (1 each, in the correct position). has_available_copy() separates the availability check into a named subproblem that the reservation function can call (1). After the first reservation, BK63 has no copies left, so the second reservation fails.
For OCR AS H046/02, Algorithms and problem solving, section 2.1.1 covers abstraction and abstract models; section 2.1.3 covers problem components and sub-procedures. The ideas also support OCR A-Level H446/02, Algorithms and programming, including problem decomposition and abstraction in section 2.2.2. “Paper 2” here refers to OCR's assessment structure; other boards arrange these topics differently.
These are independently written explanations and practice questions. CompSciTutoring.co.uk is not affiliated with or endorsed by an examination board. The marking guidance is indicative; always check the syllabus for your examination year.
Found an error? Email a correction.
Use the free examples first, then explore one-to-one tuition if guided practice would help.