SECTION 20 / 24

Keep ID, Maths and Physics together in triples

1 min read
Rida’s triple [3,42,53] keeps identity and two subject marks. The middle component is first(rest(Triple)).
Rida’s triple [3,42,53] keeps identity and two subject marks. The middle component is first(rest(Triple)).
REAL COURSE DATA · BUILD AND UNPACK LISTS

Use ID–Maths–Physics triples

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.

A triple is a list of exactly three components. For [ID,Maths,Physics], first gives ID, first(rest(Triple)) gives Maths, and last gives Physics. last(init(Triple)) is another way to obtain the middle value. A list of these triples keeps both subject marks attached to the same student.

Triples = []
foreach X in ScoreRecords:
    Triples = Triples ++ [[X.CardNo,X.Mathematics,X.Physics]]

MathsSum = 0; PhysicsSum = 0; Count = 0
foreach T in Triples:
    MathsSum = MathsSum + first(rest(T))
    PhysicsSum = PhysicsSum + last(T)
    Count = Count + 1
# If Count > 0, compute the two means. Then classify in a second pass.

The real means are Maths 2171/30 = 72.3667 approximately, and Physics 2189/30 = 72.9667 approximately. In the second pass, select students with Maths strictly above its mean and Physics strictly below its mean, OR the opposite. The exact fractions determine the comparisons; rounded displays are only for reading.

The result has 13 IDs: [4,5,8,9,13,14,15,17,18,19,20,21,26]. We describe performance relative to these subject means, not a general label about a person’s ability. A mark exactly equal to a mean would enter neither strict above/below branch. Empty input requires handling Count = 0 before dividing.

Try it yourself

How do you extract the middle value from [3,42,53]?

Show explained answer

first(rest([3,42,53])) = first([42,53]) = 42. Equivalently, last(init(...)) = last([3,42]) = 42.

Try it yourself

Why not compare a mark with a running average while collecting the sums?

Show explained answer

The final means are not yet known. Complete the aggregate pass, then classify against fixed means in a second pass.

Try it yourself

Why include ID rather than just [Maths,Physics]?

Show explained answer

The marks can still be compared, but the ID lets you identify each selected student and reconnect to their record.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑