FOUNDATION · WEEK 04

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.

17 study sections21 illustrations2 resources
Start Week 4 →

Study sections

Follow the order or choose a section
01

One 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.…

1 illustrations
02

Find 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…

1 illustrations
03

Count 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…

1 illustrations
04

Put 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…

1 illustrations
05

How 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…

2 illustrations
06

Tutorial: 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…

1 illustrations
07

Shopping 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…

1 illustrations
08

Build 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…

2 illustrations
09

Heuristics: 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…

1 illustrations
10

SearchBirthday: 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…

1 illustrations
11

Find 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…

2 illustrations
12

Remember 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…

1 illustrations
13

When 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…

1 illustrations
14

Tutorial: 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…

1 illustrations
15

Tutorial: 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…

1 illustrations
16

Tutorial: 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…

1 illustrations
17

Connect 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…

2 illustrations