FOUNDATION · WEEK 05

Lists, shared collections and structured results

Build reusable ID lists, find common students, unpack first and last elements, explore words and shopping items, and keep identities in pairs and triples. Build on Weeks 1–4 and keep useful results for the next computation.

24 study sections24 illustrations2 resources
Start Week 5 →

Study sections

Follow the order or choose a section
01

Remember a collection without removing the cards

Earlier, one variable held a count, a sum or a maximum. Now we want to remember several students together: everyone from Chennai, every…

1 illustrations
02

Lists keep order, repeats and an empty starting point

A list is a sequence written inside square brackets, with commas between its elements. [1,13,2] has three elements. ["Monday","school"]…

1 illustrations
03

Join lists and add one new element

In this course, ++ means list concatenation. If A = [1,13] and B = [2,17,1], A ++ B is [1,13,2,17,1]. No sorting or deduplication happe…

1 illustrations
04

Build May and Chennai lists from the course data

Begin with two empty results. Read each score card, apply the two filters independently and append the ID when a condition passes. Mont…

1 illustrations
05

foreach visits values in list order

x is the current element, not automatically a list index. In MayIDs, the first x is 3, even though it occupies the first position. The…

1 illustrations
06

Compare saved lists: overlap and difference

After building the two ID lists, we can find their common members without scanning every original field again. For each May ID x, compa…

1 illustrations
07

Collect everyone at the top three distinct mark levels

The earlier TopThreeMarks procedure finds distinct score levels. Maths has levels 97, 93 and 89; Physics 92, 91 and 88; Chemistry 97, 9…

1 illustrations
08

Who belongs to all three topper lists?

The first scan compares 4 × 6 = 24 identifier pairs. The second compares 2 × 4 = 8. Together these membership scans use 32 comparisons…

1 illustrations
09

Combine more lists in stages

For four collections A, B, C and D, one method is to intersect A with B, then the result with C, then that result with D. Another metho…

0 illustrations
10

Unpack a list into its first element and the rest

first(L) returns one element. rest(L) returns a list containing all occurrences after the first position. Neither operation means “remo…

1 illustrations
11

Check ascending marks with adjacent comparisons

Ascending here means non-decreasing: every mark is less than or equal to the next. Equality is allowed. Compare a neighbour pair, then…

1 illustrations
12

Collect nouns immediately after adjectives

Keep the word-card order. Let X = first(L) and Y = first(rest(L)). Select this neighbour pair only if X.PartOfSpeech is Adjective AND Y…

1 illustrations
13

Turn a word into a character list

A word stored as one field can be unpacked with explode into a character sequence. explode("It") gives ["I","t"]. You can now use forea…

1 illustrations
14

Count adjacent vowel pairs inside a word

Use the five-vowel convention a, e, i, o, u, ignoring case for this test. In specially, i is immediately followed by a, giving one matc…

1 illustrations
15

Unpack the other end: last and init

last(L) returns the last element. init(L) returns the list without its final position. For [3,13,23,25], last is 25 and init is [3,13,2…

2 illustrations
16

Is a word a palindrome?

A palindrome reads the same forwards and backwards. If the first and last characters differ, reject immediately. If they agree, remove…

1 illustrations
17

Keep an ID beside a property: lists of pairs

A list of marks alone loses the owners of those marks. A pair such as [3,53] keeps one identifier beside one property. A list of pairs…

1 illustrations
18

Find the maximum and remember every tied student

When a larger mark arrives, earlier candidates are no longer maximum scorers: replace their list with [ID]. When an equal mark arrives,…

1 illustrations
19

Store relationships as [ID1,ID2] pairs

The lecture first discusses similar performance in two subjects, then demonstrates the pair method for Physics alone. The explorer lets…

1 illustrations
20

Keep ID, Maths and Physics together in triples

A triple is a list of exactly three components. For [ID,Maths,Physics], first gives ID, first(rest(Triple)) gives Maths, and last gives…

1 illustrations
21

One bill item becomes one customer–shop–category triple

A bill contains several item rows. For each row, keep [Customer,Store,Category]. This produces one triple per ITEM ROW, not one per bil…

1 illustrations
22

Compare the same student’s positions in two lists

Make [ID,Maths] and [ID,Physics] lists, each sorted ascending by its mark. For a student at position i in the Maths list, find the SAME…

1 illustrations
23

Tutorial: implement length, first and last

length accepts a list and returns its occurrence count. The empty list returns 0 because no increment occurs. first returns during the…

1 illustrations
24

Tutorial: rest, init and membership

rest uses Found to skip exactly the first occurrence. init uses a one-element delay: when the second element arrives, append the rememb…

1 illustrations