CP1 / COURSE HOME

Week 08 / Organise observations

Find a reading in a grid

Keep the location attached to the value.

A list preserves order along one sequence. A thermal map needs two coordinates: which row and which column contain a reading?

A small thermal map has rows [22, 23, 24] and [25, 80, 26], all in °C. Rows run top to bottom and columns left to right.

Start with the problem

Try this first.

Draw the two rows, circle the hottest sample and label its row and column starting from zero. Could a report containing only “80 °C” tell someone where to look?

Why this week's tool?

Nested lists hold rows of readings. Nested loops visit every row–column pair, allowing the value and its coordinates to stay together.

By the end: Report the peak as a temperature and a row–column pair. Explain why those indices are not physical distances.

The idea behind the program

Declare the coordinate convention

grid[row][column] first selects a row, then a value within that row. A 2 × 3 grid has row indices 0–1 and column indices 0–2. State whether a report uses zero-based indices or human-friendly numbering.

Nested loops visit positions

The outer loop selects a row; the inner loop visits its columns. Track value and location together. If you remember only the maximum, you lose the location that makes the result actionable.

Shape is part of the data contract

A transpose swaps rows and columns. It preserves numeric values but changes their coordinate interpretation. Ragged rows violate a rectangular-grid assumption and need an explicit policy before processing.

A short Python example

Read the example alongside the explanation. Run it in a new notebook cell and change one input to see how it behaves.

grid_c = [[22, 23, 24], [25, 80, 26]]
peak = grid_c[0][0]
location = (0, 0)
for row in range(len(grid_c)):
    for col in range(len(grid_c[row])):
        if grid_c[row][col] > peak:
            peak = grid_c[row][col]
            location = (row, col)
print('Peak (C):', peak)
print('Zero-based row, column:', location)

Example output

Peak (C): 80
Zero-based row, column: (1, 1)

Nested indexing preserves location

grid[row][column] first chooses a row object and then an element in it. A nested traversal should preserve the coordinates associated with each reading. Index numbers are positions in a representation, not physical distances.

Draw or trace

Draw [[22, 23, 24], [25, 80, 26]] as a two-row, three-column table. Label row and column indices from zero.

Predict before running. Where is 80? What information is lost if the report returns only the largest value?

Trace and explanation — after your prediction
  1. Outer row 0 visits columns 0, 1, 2.
  2. Outer row 1 visits columns 0, 1, 2; the peak is at (1, 1).
  3. A temperature without its coordinates cannot identify the affected location.

The peak is 80 at row 1, column 1. The six visits follow the grid shape. For ragged rows, each row can have a different length; a rectangular assumption must be stated and checked.

Change one thing. Place another 80 at (0, 2). Decide whether the required result is the first maximum, last maximum or all maxima. That choice determines how equal values are handled.

Türkçe: İndis değerin konumudur, fiziksel uzaklık değildir. Eşit maksimumlarda hangi konumun döndürüleceğini önceden belirle.

Examples and variations

Each example changes something about the same problem. Open the ones you want to explore and follow the worked explanation.

01Original map

Rows: [22, 23, 24] and [25, 80, 26] °C

Question: Locate the maximum using zero-based row and column.

  1. Row 0: candidate peaks 22 → 23 → 24
  2. Row 1: 25 → 80; 26 does not exceed 80
  3. Store value 80 with location (1, 1)

80 °C at row 1, column 1

In human numbering this is the second row and second column. Say which convention you use.

02Off-centre hot spot

Rows: [22, 23, 90] and [25, 24, 26] °C

Question: Would swapping row and column give a valid original position?

  1. Maximum 90 is at (0, 2)
  2. Swapped report would be (2, 0)
  3. Row 2 does not exist in this two-row grid

90 °C at (0, 2); swapped coordinate is invalid

An asymmetric case catches a row/column mistake that the original (1, 1) case hides.

03Tied peaks

Rows: [80, 23, 24] and [25, 80, 26] °C

Question: Which peak does the model keep with a strict > comparison?

  1. Initial peak 80 at (0, 0)
  2. Later 80 does not satisfy 80 > 80
  3. First location is retained

80 °C at (0, 0); a second equal peak exists

The model returns the first maximum, not every hot spot. A report should disclose that tie policy.

See the Colab code run

Interactive walkthroughs of 2D Lists & Nested Loops. Enable JavaScript to step through code, variables, collections and output. The companion notebook remains available below.

Work on it in Colab

Use the notebook to try the ideas yourself. The steps below connect this week's example to the programming practice.

  1. Draw the 2 × 3 grid on paper and mark row/column directions. Trace the first two iterations by hand.
  2. Run the original and off-centre cases. Explain why the second case is a stronger test of coordinate handling.
  3. Introduce tied maxima. Compare > with >= and describe which location each version keeps.
  4. Write a location report that states the coordinate convention, tie policy and absence of a physical scale.

Something to take away: An annotated map, the peak with coordinates, and a test that exposes a swapped-coordinate implementation.

Suggested exercises, downloads & solutions

Read the notebook's teaching cells before these exercises.

  • EX03 Row Sums (Medium)
  • EX04 Column Sums (Medium)
  • EX05 Find Maximum Element (Medium)
Download notebookWorked solutions

Use the notebook's core and optional labels to choose your workload. This activity fits within guided class time.

Optional notes & guidance
My notes
Using AI or working with a partner

Ask AI to locate the maximum in a grid. Test it on a non-square grid with an off-centre maximum and on tied peaks. Ask it to state its tie policy instead of claiming it found all hot spots.

You can also review the supplied example with a partner. Use the same inputs to compare the reasoning. Follow the syllabus rules for assessed work.