SECTION 04 / 24

Build May and Chennai lists from the course data

1 min read

Begin with two empty results. Read each score card, apply the two filters independently and append the ID when a condition passes. MonthOfBirth is derived from DateOfBirth; it is not a separate column in our dataset. The source uses SeqNo and TownCity; here the corresponding fields are CardNo and CityTown.

In the original CardNo order, the May IDs are [3,13,23,25]: Rida, Gopi, Deepika and Geeta.
In the original CardNo order, the May IDs are [3,13,23,25]: Rida, Gopi, Deepika and Geeta.
REAL COURSE DATA · BUILD AND UNPACK LISTS

Build May and Chennai ID lists

Follow one occurrence at a time. Choose another input to test empty lists, repeated IDs and different parameters.

Ready

CURRENT RECORD / VALUE

Ready to begin

No records have been processed. Start with Next step.

What we remember

Processed
0

Predict the next change, then step through the list. The original course records remain unchanged.

MayIDs = []
ChennaiIDs = []
foreach X in ScoreRecords:
    if month(X.DateOfBirth) == "May":
        MayIDs = MayIDs ++ [X.CardNo]
    if X.CityTown == "Chennai":
        ChennaiIDs = ChennaiIDs ++ [X.CardNo]
CollectionIDs in the dataset scan order
May[3,13,23,25]
Chennai[2,3,5,10,12,17,25,26]
Both[3,25]

One pass can build both lists. Separate passes can also work, provided the records are available again. A physical Table1-to-Table2 implementation must restore or rename its tables according to its contract before another full scan. A read-only scan of an existing record list does not consume it.

Each unique score record is scanned once, so each qualifying CardNo enters each collection once. Different source orders can produce differently ordered result lists while still representing the same membership. We keep the original dataset order for consistent examples.

Try it yourself

Why use two IFs rather than IF…ELSE IF?

Show explained answer

A student can satisfy both filters. ELSE IF would prevent a May-born Chennai student from entering the second list.

Try it yourself

What should the result be when no student passes a filter?

Show explained answer

An empty list [], not an uninitialized variable and not [0].

Try it yourself

If you only store names, what problem might occur in a larger class?

Show explained answer

Different students may share a name. Stable unique IDs make membership comparisons unambiguous.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑