4.13.1.3–4.13.1.4 - Implementing, debugging and testing a solution (Python)
Turn a design into Python data and instructions, then check whether the implementation behaves as required. You will debug a small booking function, choose useful tests and distinguish evidence of correctness from acceptance by its users.
Implementing models and algorithms
A design becomes an implementation when its model is represented by data structures and its algorithm is expressed as instructions the computer can execute. A diagram of a booking process does not itself reserve seats: the program must store the available count, interpret a request, apply the conditions and update the count.
For a small booking prototype, use an integer for remaining and a list of strings for incoming requests. The algorithm accepts an integer request from 1 to 8 only when enough seats remain. Rejected requests leave the state unchanged. A function returning a pair (accepted, new_remaining) makes both the decision and the change visible to its caller.
Booking decisions and state
{"code":"MAX_REQUEST = 8\n\ndef reserve(raw, remaining):\n try:\n quantity = int(raw)\n except ValueError:\n return False, remaining\n if not 1 <= quantity <= MAX_REQUEST or quantity > remaining:\n return False, remaining\n return True, remaining - quantity\n\nremaining = 10\nfor raw in ["3", "8", "0", "x", "7"]:\n accepted, remaining = reserve(raw, remaining)\n print(raw, accepted, remaining)","instructions":"Predict the decision and remaining count for each request, then run. Explain why the rejected request for 8 does not consume seats. Change MAX_REQUEST to 6 and replace the requests with ["6", "5", "4"]. Predict, run and explain which requirement changes and which rule stays the same.","files":[],"original_filename":"main.py","runtime_profile":"python-console-v1","file_mode":"fresh"}
The original run accepts 3, rejects 8 because only 7 remain, rejects 0 and x, then accepts 7. The modification changes the maximum size of a single request; it still checks availability. The state belongs to the caller, and each result is used before the next request. Merely calling reserve without using its returned count would lose the update.
Debugging locates and removes defects. A syntax error stops parsing; a runtime error occurs during execution; a logic error produces behavior that contradicts a requirement. In the task below, comparing exact expected and actual results exposes incorrect conversion, boundary and state-update logic. Repair a cause, then rerun the failing case and cases that previously worked: this is regression testing.
Critical-path prototyping
An iterative process develops a solution through repeated cycles of building, testing, obtaining feedback and revising. A prototype is a small or simplified implementation used to resolve an uncertainty. It may be replaced later or developed into the final solution.
Solve the critical path first: establish that the essential chain of operations needed to solve the problem is feasible before spending time on less important features. For a venue booking system, storing availability, accepting a valid request and preserving the correct remaining count matter before colour themes or decorative reports. If two requests can incorrectly reserve the same last seat, a polished interface cannot repair that failure.
An agile approach delivers small increments and uses feedback to refine the next one. An initial command-line prototype can establish the booking rule; a later increment can persist bookings in a file; another can improve the interface after staff use it. Each increment has a testable requirement and leaves evidence of what works. Iteration is purposeful change, rather than repeatedly editing code with no expected outcome.
Changing a requirement requires revisiting its dependent design and tests. Reducing the maximum request from 8 to 6 changes the boundary cases. It does not justify deleting the availability tests. A useful record links the observed problem, the decision to change it, the revised version and the result of checking it.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account