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.
Study sections
Follow the order or choose a sectionA 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.…
02Organize 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…
03Keep 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…
04Insert 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.…
05Handle 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…
06Use 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…
07Build 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…
08Sort 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…
09Read < 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…
10Sorting 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…
11Equal 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…
12Count 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…
13Run 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…
14Keep a tie policy explicit
The manual lecture activity moves boundaries to avoid giving equal marks different grades. The later pseudocode intentionally omits thi…
15Unpack 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…
16Define “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…
17Reset 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…
18A 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…
19Tutorial: 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…
20Tutorial: 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’…
21Meet 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…
22Use 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…
23Use 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…
24Shift 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…
25Search 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…
26Identify 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…
27Compare 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…
28Keep 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…
