FOUNDATION · WEEK 06

Nested lists, insertion sort and train timetables

Sort reusable views, group equal values, test precise grade hypotheses and explore historical train records through easy explanations and interactive traces. Build on Week 5 lists and use ordered, structured results for new questions.

28 study sections24 illustrations2 resources
Start Week 6 →

Study sections

Follow the order or choose a section
01

A paragraph can be a list of lists of lists

Last week, a pair was a list with two elements and a triple had three. Nested lists can also contain inner lists of different lengths.…

1 illustrations
02

Organize customers, shops and bill lists

A flat customer–shop–category list can be regrouped hierarchically. At the top choose a customer; inside that customer choose a shop; i…

0 illustrations
03

Keep the cards unchanged and sort separate views

Sorting arranges values by a chosen comparison rule. Ascending order puts smaller keys first; descending puts larger keys first. Non-de…

1 illustrations
04

Insert one new value into a sorted list

Insertion assumes the existing list is already sorted by the same rule. Read it from the beginning, copying old values into a new list.…

1 illustrations
05

Handle empty, first and final insertion positions

Three boundary cases test the algorithm. On [], the foreach body runs zero times, so a final fallback must produce [x]. If x is smaller…

1 illustrations
06

Use inserted to place x exactly once

The flag remembers an event across loop iterations. Before insertion, test where x belongs. After insertion, stop testing x against new…

1 illustrations
07

Build insertion sort and explain why it works

An invariant is a property maintained after each completed iteration. Here sortedList is sorted AND contains exactly the input occurren…

1 illustrations
08

Sort by one component and keep the whole pair

The outer list contains pairs; first(pair) is the ID and last(pair) is the selected mark. Comparing entire pair objects may use the wro…

1 illustrations
09

Read < and ≤ carefully when marks or pages tie

The lecture’s ascending insertion uses x < z. An incoming item with an equal key passes old equals and is inserted after them. If every…

1 illustrations
10

Sorting costs work, but makes later queries easier

Sorting is preparation. Once a mark list is sorted, endpoints give extrema and positions help select high or low values. A descending l…

0 illustrations
11

Equal birthdays become neighbouring records

Normalize month spellings, then sort by calendar month and day number. Do not sort raw date strings alphabetically: “12 Jan” and “4 Jan…

1 illustrations
12

Count sorted groups and flush the last run

Sorting gathers equal keys into runs. Keep the current key and a count. On a key change, save the previous group, reset the count and b…

1 illustrations
13

Run the source’s exact positional grade rule

Read the comparisons literally. For 30 students the boundaries are 7.5,15,22.5. Starting at position 0 gives D at 0–7, C at 8–15, B at…

1 illustrations
14

Keep a tie policy explicit

The manual lecture activity moves boundaries to avoid giving equal marks different grades. The later pseudocode intentionally omits thi…

1 illustrations
15

Unpack the four returned grade lists

The grading procedure returns one outer list containing four lists. Its outer length is 4 even though there are 30 IDs across the inner…

1 illustrations
16

Define “does at least as well” before counting

The statement “students who do well in Maths do at least as well in Physics” needs a measurable rule. Here doing well means Maths grade…

1 illustrations
17

Reset found and leave only the inner search

One non-match does not prove absence. Keep searching allowed destinations until a match appears or every candidate is exhausted. True c…

1 illustrations
18

A confirmation fraction is descriptive evidence

The lecture uses confirmation versus rejection as a systematic way to examine a hypothesis. It is useful to state the eligible group, t…

1 illustrations
19

Tutorial: trace the numbered book-insertion code

The exercise describes a library table of 1,000 books, but the concrete insertion example supplies four pairs: [[5,220],[10,350],[15,35…

1 illustrations
20

Tutorial: read pair selectors from the inside out

The last expression is an identifier, not the least page count. Values can accidentally coincide numerically; answer by the expression’…

1 illustrations
21

Meet the historical train-card dataset

The new dataset has train-wise cards and station-wise cards. A train card names one service number, its origin operating weekdays and a…

1 illustrations
22

Use a station card to find relevant train cards

A station card lists services calling at that station. It is an index into the train cards: begin with relevant train numbers rather th…

1 illustrations
23

Use journey days when subtracting times

Clock text alone cannot distinguish the next morning from the previous morning. On 02201, Kolkata Day 1 20:00 to Bhubaneswar Day 2 03:3…

1 illustrations
24

Shift origin weekdays to each station call

Operating weekdays on a train card refer to the origin departure. Journey Day 2 is one calendar day later. Thus 02201’s Monday, Wednesd…

1 illustrations
25

Search direct routes with direction and weekday

The outer loop examines candidate trains; the inner scan examines their station lists. Source must occur before destination in that dir…

1 illustrations
26

Identify the station’s role for each train

On the supplied cards, a source call has a departure but no arrival; a destination has an arrival but no departure; an intermediate cal…

0 illustrations
27

Compare next calls, but interpret the clue carefully

The lecture compares different services calling at a station, excluding a service and its paired return as the sole evidence. If their…

1 illustrations
28

Keep and flag inconsistent source records

The lecture stresses that train and station cards should agree after accounting for journey-day offsets. Actual supplied cards also pro…

0 illustrations