Community resourceWorksheet
1CP2-CT-4.6 Linear search through records
Part 6 of 7 · 1CP2-CT-4 · Structures, validation and search
The record search worksheet, comparing one field while returning another.
Students will:
- compare a key field within each record
- explain why the search compares one field and prints another
- construct a record search that handles an absent target
- give a normal, a boundary and an absent test with expected results
- explain why every record must share the same field positions
Inside: 6 explanation cells, 3 multiple-choice questions, 1 Python task, 1 fill-in-the-blanks cell and 2 written answers. 16 marks, about 45 minutes.
Series: 1CP2-CT-4 · Structures, validation and search, part 6 of 7.
Shared by Coding PathwayVerified teacher
- 13 cells
- About 45 minutes
- CC BY-SA 4.0
- Shared 17 Aug 2026
Preview
The whole resource, exactly as a class sees it. Answers and marking are held back.
Linear search through records
A two-dimensional list can represent a list of records. To find one record, a linear search normally compares one key field in each row, then uses the whole row when the key matches.
1. Search the key, return the record
The outer index identifies the current record. The second index identifies the key field: records[index][0]. Comparing every field would be unnecessary and could confuse the identity of the record with its other data.
records[index][0], index selects the gap 2 and 0 selects the gap 3.- field
- key
- record
- target
For records [ID, item, quantity], which expression compares the current record's ID with target_id?
- Arecords[0][index] == target_id
- Brecords[index] == target_id
- Crecords[index][0] == target_id
- Drecords[index][2] == target_id
2. Follow a record search
records = [["R14", "Cable", 8],
["R27", "Mouse", 5],
["R31", "Keyboard", 3]]
target_id = "R27"
index = 0
while index < len(records) and records[index][0] != target_id:
index = index + 1
print(records[index][1])
What does the listing print?
- A`R27`
- B`Mouse`
- C`5`
- D`Keyboard`
Explain why the search compares field 0 but prints field 1.
Describe the purpose of each field.
Students type their answer here.
3. Construct a safe record search
Search members for target_id. Use a bounded while loop and a found Boolean. If found, store the whole matching row in matched_record; otherwise store an empty list. Print matched_record.
members = [["M01", "Ari", True], ["M04", "Bo", False], ["M09", "Cy", True]]
target_id = "M04"
# Search field 0 safely and store the matching record.
4. Decompose before coding
A record search has four responsibilities: obtain or receive the target key; traverse candidate rows; compare the correct field; communicate the matched record or absence. Keeping these responsibilities visible improves testing and later subprogram design.
Give one normal, one boundary and one absent test for a search of three member records, and state the expected outcome of each.
Use target positions and a not-found case.
Students type their answer here.
Why must every core record row contain the same fields in the same positions?
- ASo indexes identify consistent meanings and the structure is rectangular.
- BSo every field must have the same data type.
- CSo linear search can only use sorted data.
- DSo the list cannot be changed.
Route forward
You can adapt linear search to a rectangular list of records, select a key field and return the whole matching row safely. The checkpoint now combines the 1CP2-CT-4 skills only.