Lesson 7.5 · Write it
First hot day
A functionPython or Java9 tests, 6 of them hidden
On a phone? Put the lines in order instead
A weather station keeps one temperature a day, in whole degrees, in a list (an array in Java). Day 0 is the first day. Write a function that finds the first day that was hotter than a limit.
- Return the first day whose temperature is greater than
limit. - A temperature equal to the limit is not hotter than it.
- If no day was hotter, return
-1. - The list can be empty, and temperatures can be below zero.
Only the value your function returns is checked.
def first_day_over(temps: list[int], limit: int) -> int
Examples
Your function is tested on 9 cases: the 3 examples below, and 6 hidden ones that check the rules above.
- first_day_over([21, 24, 31, 28, 33], 30) should return 2: day 2 is the first day over 30 (rule 1)
- first_day_over([21, 24, 19], 30) should return -1: no day is over the limit (rule 3)
- first_day_over([30, 30, 31], 30) should return 2: days equal to the limit do not count (rule 2)
public static int firstDayOver(int[] temps, int limit)
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.
Examples
Your function is tested on 9 cases: the 3 examples below, and 6 hidden ones that check the rules above.
- firstDayOver(new int[]{21, 24, 31, 28, 33}, 30) should return 2: day 2 is the first day over 30 (rule 1)
- firstDayOver(new int[]{21, 24, 19}, 30) should return -1: no day is over the limit (rule 3)
- firstDayOver(new int[]{30, 30, 31}, 30) should return 2: days equal to the limit do not count (rule 2)