From a Requirement to Executable Steps
Unit ID: M04-U00 Estimated active time: 20-30 minutes
Start before the syntax
Consider this requirement:
Review each supplied study record. Accept records with a non-empty ID and an integer hour value from 0 to 40. Keep rejected records with a reason.
Do not begin by typing if and for. First write the decisions in plain language.
A plain-language algorithm
- Prepare an empty accepted list and an empty rejected list.
- Examine each record in the supplied order.
- Read its ID and hour value.
- If the ID is not valid, record an ID reason.
- If the hour value is not an integer, record a type reason.
- Otherwise, if the integer is outside 0 to 40, record a range reason.
- If there are no reasons, add a copy to accepted records.
- Otherwise, add the record ID and reasons to rejected records.
- Report counts and totals.
This algorithm separates iteration, decisions, state changes, and output.
Name the inputs and outputs
Before coding, state:
- input: supplied list of dictionaries;
- accepted output: list of valid record copies;
- rejected output: list containing the original position or ID and reasons;
- summary: accepted count, rejected count, and accepted hours total.
Clear output definitions stop the code from silently discarding evidence.
Find the repeated step
The phrase "examine each record" signals repetition. A for loop will later express it.
The phrases beginning with "if" signal conditions. Boolean expressions and branches will express them.
The prepared lists and totals are state that changes while the program runs.
Trace one record manually
Record:
{"id": "R2", "hours": -2}
Trace:
- ID is a non-empty string: no ID reason.
- Hours has exact type
int: no type reason. - Hours is below zero: add the range reason.
- Reasons exist: place the record in rejected output.
Manual tracing gives you an expected result before code exists.
Practice
Write a plain-language algorithm for this task:
Given a list of module scores, count scores at or above 8, count scores below 8, and calculate the total of valid scores from 0 to 10. Preserve invalid scores separately.
State the input, three outputs, repeated step, decisions, and boundary values.
Takeaway
Problem decomposition turns a vague request into named inputs, outputs, repeated steps, decisions, and state. Next, we will make the decision rules precise with boolean logic.
