අන්තර්ගතයට යන්න
mcqdinethlive
SPARK · UNIT 9

Sequential search in Python 101

A sequential search returns inside the loop on a match and gives the not found value only after the loop. 2026 Paper II Q9(c) shows where that line goes.

සිංහලෙන් කියවන්න

A sequential search in Python has one rule for the "not found" value: it comes after the loop, never inside it. The search checks the items one by one from the start (Teachers' Guide 9.13, page 84) (ගුරු අත්පොත 9.13, පිටුව 83). It returns as soon as one item matches. Only when the loop ends with no match does it give the "not found" value.

2026 Paper II, B, Q9(c) asks for searchFine(fineList, memberNo), which returns a member's fine, or -1 when the member is not in the list. In our own worked answer, the loop holds if record[0] == memberNo: return record[1], and return -1 lines up with for, after the loop. A fine is never negative, so -1 cannot be mistaken for a real fine.

The common slip puts else: return -1 under the if. Then the function returns on the first record, match or not. In the list [("M003", 50), ("M004", 425), ("M005", 500)], a search for M004 stops at M003 and returns -1. So line up return -1 with for, not with if. The full question, with the function, the list of tuples and the fine rule that can be read two ways, is in Python functions with lists of tuples and sequential search.

මේ Spark එක බෙදාගන්න

A/L ICT කරන යාළුවෙකුට යවන්න.

තවත් Sparks

  1. English

    Layer 2 and Layer 3 switches 101

    A Layer 2 switch forwards frames by MAC address inside one LAN, and a Layer 3 switch also routes packets by IP address between networks. Why both exist, where they work today, and the past paper questions that test them.

  2. English

    Planning an A/L ICT second attempt

    A low 2026 A/L ICT result is a map of gaps, not an end. Six units carry 73.4% of the marks, and every paper for the plan is free on this site.

  3. English

    Normalisation by dependencies 101

    The normal form of a table comes from its arrows. Find the key, draw who decides each column, and the kind of arrow names the normal form and the split.

All Sparks