Nested loops, useful bins and thoughtful shortcuts
Find birthday pairs, compare cities and marks, group shopping bills, explore study partners and test pronoun rules with real course data. Connect the patterns from Weeks 1–3 and prepare for the four-week review.
Study sections
Follow the order or choose a sectionOne loop inside another: nested iteration
You already know how to go through a pile once. Now imagine holding one student card, X, while reading several other student cards, Y.…
02Find students with the same birthday
For this task, “same birthday” means the same day number AND the same month. The year does not matter. Two different students born on 1…
03Count pairs without counting twice
Start with n × n choices. Remove the n self-comparisons. There are now n(n−1) ordered pairs. A–B and B–A represent the same unordered p…
04Put birthdays into month bins
A bin is simply a labelled group. First put each card into its month bin: January, February and so on. Two students born in different m…
05How much work does binning save?
For a bin containing nᵢ items, the pair count is nᵢ(nᵢ−1)/2. Add that number over all bins. This works even when the bins are not the s…
06Tutorial: same city AND same Maths mark
Find pairs of different students with both the same CityTown and the same Mathematics mark. Checking only city counts too many pairs. C…
07Shopping bills: find loyal customers
In this lecture, a loyal customer has at least two observed bills, all from the same shop. One visit is too little evidence for this de…
08Build study pairs that help each other
A useful study pair can help in both directions: one student has stronger Maths marks, while the other has stronger Physics marks. This…
09Heuristics: useful choices, limits and better bins
A heuristic is a practical rule that tries to find a good answer with less search. It is not automatically an exact method or a proof o…
10SearchBirthday: inputs, return values and side effects
A procedure lets the caller delegate a well-defined task. SearchBirthday(Target, OtherCards) returns true if at least one card in Other…
11Find a pronoun’s person: search backwards
A pronoun such as he or his can refer to a person mentioned earlier. Our word cards preserve the paragraph’s order with SerialNumber. T…
12Remember a name instead of searching again
Repeatedly searching backwards for Swaminathan seems wasteful. Keep a variable LastName. Start it as empty. Read forwards; when a recog…
13When the nearest-name rule gets meaning wrong
At position 55, the teacher is Vedanayagam, but “his class teacher” means Swaminathan’s class teacher. At position 61, the cane belongs…
14Tutorial: OR is not always two separate IFs
A OR B is true when at least one condition is true, including when both are true. A AND B requires both. An IF with an OR condition run…
15Tutorial: one combined count or two separate counts?
In these tagged word cards, an occurrence has one PartOfSpeech value, so Noun and Verb are mutually exclusive labels. Counting Noun OR…
16Tutorial: count vowels and consonants in a word
CountVowelsAndConsonants(X) receives one word card. For “reluctant”, start i = 1, Vcount = 0 and Ccount = 0. Inspect the ith letter. If…
17Connect Weeks 1–4: choose the right pattern
A record is one card with named fields: Name, DateOfBirth, marks and so on. A list is an ordered collection of values or records. Integ…
