Skip to content
ExamSpine

Write it

Warmest day

A functionPython or Java12 tests, 7 of them hidden

On a phone? Put the lines in order instead

The school weather station keeps a week of hourly readings in a two-dimensional list (a 2D array in Java) called grid. Each row is one day and each value in a row is one hourly temperature in whole degrees Celsius, so grid[2][5] is the reading for day 2, hour 5.

Write a function that returns the number of the day (the row index) whose readings add up to the highest total. Days are numbered from 0.

  1. A day's total is the sum of every reading in its row.
  2. If two or more days share the highest total, return the first of them.
  3. grid has at least one row, every row has at least one reading, and rows can have different lengths.
  4. Readings are between -60 and 60, and a row has at most 24 readings, so totals can be below 0.
  5. Write the adding and the comparing yourself. The shortcuts listed with this question are not allowed.

Only the number your function returns is checked.

def warmest_day(grid: list[list[int]]) -> int

You cannot use these in this question:

  • sum()
  • max()
  • min()
  • sorted()
  • .sort()
  • .index()
  • import statistics
  • import functools
  • import heapq

Paper 2 questions on 2D arrays expect you to write the nested loops that add each row yourself, so practise them without the built-ins.

Examples

Your function is tested on 12 cases: the 5 examples below, and 7 hidden ones that check the rules above.

  • warmest_day([[1, 2, 3], [4, 5, 6], [0, 0, 1]]) should return 1: day 1 totals 15, the highest
  • warmest_day([[5, 5], [2, 8], [3, 3]]) should return 0: days 0 and 1 both total 10, so the first wins (rule 2)
  • warmest_day([[-3, -4], [-1, -2], [-5, 0]]) should return 1: every total is below 0 (rule 4)
  • warmest_day([[1, 1], [9, -9, 1], [3, 0]]) should return 2: day 1 has the hottest single hour, but day 2 has the highest total (rule 1)
  • warmest_day([[5, 5, 5], [6, 6]]) should return 0: day 1 has the higher average, but day 0 has the higher total (rule 1)

public static int warmestDay(int[][] grid)

Checking whether Java answers can run here.

You cannot use these in this question:

  • Math.max()
  • Math.min()
  • Arrays.stream()
  • Arrays.sort()
  • IntStream.of()
  • .stream()
  • .sum()
  • .max()
  • .min()
  • .sort()
  • import java.util.stream

Paper 2 questions on 2D arrays expect you to write the nested loops that add each row yourself, so practise them without the built-ins.

Examples

Your function is tested on 12 cases: the 5 examples below, and 7 hidden ones that check the rules above.

  • warmestDay(new int[][]{new int[]{1, 2, 3}, new int[]{4, 5, 6}, new int[]{0, 0, 1}}) should return 1: day 1 totals 15, the highest
  • warmestDay(new int[][]{new int[]{5, 5}, new int[]{2, 8}, new int[]{3, 3}}) should return 0: days 0 and 1 both total 10, so the first wins (rule 2)
  • warmestDay(new int[][]{new int[]{-3, -4}, new int[]{-1, -2}, new int[]{-5, 0}}) should return 1: every total is below 0 (rule 4)
  • warmestDay(new int[][]{new int[]{1, 1}, new int[]{9, -9, 1}, new int[]{3, 0}}) should return 2: day 1 has the hottest single hour, but day 2 has the highest total (rule 1)
  • warmestDay(new int[][]{new int[]{5, 5, 5}, new int[]{6, 6}}) should return 0: day 1 has the higher average, but day 0 has the higher total (rule 1)
solution.pySolution.java