Edexcel International Advanced Level Computer Science, point 19.1.1
Topic 19 repeats the best-practice ideas of Topic 9, but applied to more complex problems in the Unit 4 practical exam. The principles are explained in 9.1.1, so this page shows them working on a larger program. The specification points to the Good Programming Practice Guide (GPPG), which isn't reproduced here.
The problem
A small library lets members borrow books for 14 days. A member may have at most 3 books on loan. Late returns cost 10p per day. The program must record loans and returns, and report any overdue books and the fine due.
a. Problem decomposition into component parts
Break the problem into parts that each have one job:
Each box becomes one subprogram (or a few).
b. Clear and appropriate logic
Write each rule as a precise condition, and check the order and the boundaries.
| Rule | Condition |
|---|---|
| At most 3 books | Refuse if the member already has 3 (>= 3), not 4 |
| Book available | Not already on loan |
| Late | Returned after the due date: days_late = max(0, returned - due) |
| Fine | 10 * days_late pence (0 if not late) |
A boundary to test: a book returned exactly on the due date is not late (days_late is 0).
c. Variables, constants, data types and data structures appropriate to the problem
| Data | Choice | Reason |
|---|---|---|
| Loan period, limit, fine rate | Named constants | Meaningful and changeable in one place |
| Dates | Whole day numbers (or datetime.date) |
Easy subtraction |
| Fines | Integer pence | No float rounding errors |
| Books on loan | Dictionary {book_id: (member, due_day)} |
Quick look-up by book |
| Members' loan counts | Dictionary {member: count} |
Quick checks of the limit |
| Book titles | Dictionary {book_id: title} |
Look up by ID |
d. Programming constructs appropriate to the problem
if/eliffor the rules, with guard clauses for refusals.forto go through all loans in the overdue report.try/exceptfor bad input (an unknown book ID).- Subprograms for each job.
The solution
LOAN_DAYS = 14
MAX_LOANS = 3
FINE_PER_DAY_PENCE = 10
loans = {} # book_id -> (member, due_day)
loan_counts = {} # member -> number of books on loan
def lend(book_id, member, today):
if book_id in loans:
return "Book already on loan"
if loan_counts.get(member, 0) >= MAX_LOANS:
return "Loan limit reached"
loans[book_id] = (member, today + LOAN_DAYS)
loan_counts[member] = loan_counts.get(member, 0) + 1
return "OK"
def give_back(book_id, today):
if book_id not in loans:
return None # not on loan
member, due = loans.pop(book_id)
loan_counts[member] -= 1
days_late = max(0, today - due)
return days_late * FINE_PER_DAY_PENCE
def overdue(today):
return [(b, m, today - due) for b, (m, due) in loans.items()
if today > due]
Tests (normal, boundary and erroneous):
print(lend("B1", "Amy", 1)) # OK (due day 15)
print(lend("B1", "Raj", 2)) # Book already on loan
for b in ("B2", "B3"):
lend(b, "Amy", 1)
# Loan limit reached (3 already)
print(lend("B4", "Amy", 1))
# 0: returned ON the due date
print(give_back("B1", 15))
print(give_back("B2", 20)) # 50: 5 days late x 10p
print(give_back("B9", 20)) # None: not on loan
print(overdue(30)) # [('B3', 'Amy', 15)]
Results explained: B1 is due on day 15 (1 + 14), so returning on day 15 costs nothing (the boundary). B2 is 5 days late, so 50p. B3 is still on loan, 15 days overdue at day 30.
Checking the outcomes
- Run the tests and compare with hand-worked answers.
- Check the requirements one by one: limit of 3? Fine of 10p a day? Overdue report? Yes.
- Check unusual cases: returning a book that isn't on loan, lending an already borrowed book.
Exam tip: break the problem down first (a short list of subprograms), then choose data structures by how the data is looked up. Name constants for every rule's number. Test the boundaries (3rd and 4th loan, due date and one day late).
Check yourself
Why store the fine as integer pence?
Integers are exact, so there are no floating-point rounding errors in money calculations.
Why is a dictionary a good choice for books on loan?
Books are looked up by ID, and a dictionary gives fast look-up by key.
What boundary should be tested for the fine?
A book returned exactly on the due date, which must cost nothing.
Why decompose the problem before coding?
Each part has one job, so it is easier to write and test, and the whole program is easier to understand and correct.