SECTION 19 / 24

Store relationships as [ID1,ID2] pairs

2 min read
On this page 1 headings
A pair can connect two students rather than an ID and a mark. Store each unordered relationship once with ID1 < ID2.
A pair can connect two students rather than an ID and a mark. Store each unordered relationship once with ID1 < ID2.
REAL COURSE DATA · BUILD AND UNPACK LISTS

Store pairs of similar students

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.

SimilarPairs = []
foreach X in ScoreRecords:
    foreach Y in ScoreRecords:
        if X.CardNo < Y.CardNo AND abs(X.Physics-Y.Physics) <= 10:
            SimilarPairs = SimilarPairs ++ [[X.CardNo,Y.CardNo]]

The lecture first discusses similar performance in two subjects, then demonstrates the pair method for Physics alone. The explorer lets you choose either rule. Physics difference ≤ 10 gives 181 distinct unordered matching pairs. Requiring BOTH Maths and Physics differences ≤ 10 gives 67. Equality at ten is included here.

ID1 < ID2 excludes self-pairs and reversed duplicates. The displayed pseudocode visits all ordered combinations but filters its output. The explorer generates only i<j candidate pairs, so it performs 435 pair checks. Do not confuse unique stored pairs with the number of comparisons made by a less selective loop structure.

Similarity need not be transitive

Marks 74 and 81 differ by 7; 81 and 85 differ by 4. Yet 74 and 85 differ by 11. So “within ten” does not guarantee transitivity. This numerical counterexample illustrates the rule and is not a claim that these three marks identify particular Physics cards in our dataset.

Stored pairs let us later count each student’s connections or search for triples whose three pairwise connections all exist. When pairs are stored in canonical smaller-ID-first order, lookups must respect that convention. A triangle of pairwise connections is stronger than merely finding a chain A–B–C. Finding a largest fully connected group is a further, harder problem.

Try it yourself

Why is checking ID1 != ID2 insufficient to remove reversed duplicates?

Show explained answer

It removes self-pairs but allows both [5,12] and [12,5]. A strict ordering such as ID1 < ID2 retains one orientation.

Try it yourself

Does A close to B and B close to C prove A close to C?

Show explained answer

No. The 74,81,85 example breaks that inference for an inclusive ten-mark threshold.

Try it yourself

Does length(SimilarPairs) count students?

Show explained answer

No. It counts stored relationships. One student can participate in many pairs.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑