Write it
Biggest rise
A functionPython or Java12 tests, 7 of them hidden
On a phone? Put the lines in order instead
A school weather station records the temperature once an hour, in whole degrees Celsius, and stores one day's readings in order in a list (an array in Java) called temps.
Write a function that returns the biggest rise in temperature from one hour to the next hour.
- A rise is a reading minus the reading one hour before it, such as
temps[3] - temps[2]. - Only rises between hours next to each other count, including the rise into the last hour.
- If the temperature never goes up from one hour to the next, return
0. A fall is never a rise, however big it is. tempsalways has at least two readings, each between -60 and 60.- Write the comparison yourself. The shortcuts listed with this question are not allowed.
Only the number your function returns is checked.
def biggest_rise(temps: list[int]) -> int
You cannot use these in this question:
- max()
- min()
- sorted()
- .sort()
- import heapq
Paper 2 questions often ask you to find the largest value yourself, without a built-in, so practise the loop and the comparison.
Examples
Your function is tested on 12 cases: the 5 examples below, and 7 hidden ones that check the rules above.
- biggest_rise([4, 6, 5, 9]) should return 4: from 5 to 9 is the biggest rise
- biggest_rise([10, 2, 3]) should return 1: the fall of 8 is not a rise (rule 3)
- biggest_rise([8, 7, 4, 3]) should return 0: the temperature falls every hour, so return 0 (rule 3)
- biggest_rise([1, 2, 1, 6]) should return 5: the biggest rise is into the last hour (rule 2)
- biggest_rise([0, 10, 11, 8]) should return 10: the biggest rise comes first, and a smaller rise comes later
public static int biggestRise(int[] temps)
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.Checking whether Java answers can run here.
You cannot use these in this question:
- Math.max()
- Math.min()
- Arrays.sort()
- Collections.max()
- Collections.min()
- .sort()
- .stream()
- .max()
- .min()
- import java.util.stream
Paper 2 questions often ask you to find the largest value yourself, without a built-in, so practise the loop and the comparison.
Examples
Your function is tested on 12 cases: the 5 examples below, and 7 hidden ones that check the rules above.
- biggestRise(new int[]{4, 6, 5, 9}) should return 4: from 5 to 9 is the biggest rise
- biggestRise(new int[]{10, 2, 3}) should return 1: the fall of 8 is not a rise (rule 3)
- biggestRise(new int[]{8, 7, 4, 3}) should return 0: the temperature falls every hour, so return 0 (rule 3)
- biggestRise(new int[]{1, 2, 1, 6}) should return 5: the biggest rise is into the last hour (rule 2)
- biggestRise(new int[]{0, 10, 11, 8}) should return 10: the biggest rise comes first, and a smaller rise comes later