Lesson 5.8 · Write it
Count above a limit
A functionPython or Java9 tests, 6 of them hidden
On a phone? Put the lines in order instead
Write a function that returns how many values in a list (an array in Java) of whole numbers are greater than a limit.
- Only values strictly greater than
limitcount. A value equal tolimitdoes not. - An empty list returns
0. - The values and the limit can be negative.
Only the number your function returns is checked.
def count_above(values: 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.
- count_above([3, 8, 5, 10], 5) should return 2: 8 and 10 count; 5 does not, because it equals the limit (rule 1)
- count_above([], 4) should return 0: an empty list (rule 2)
- count_above([9, 2, 7], 5) should return 2: 9 and 7 count, the first value as well as the last (rule 1)
public static int countAbove(int[] values, 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.
- countAbove(new int[]{3, 8, 5, 10}, 5) should return 2: 8 and 10 count; 5 does not, because it equals the limit (rule 1)
- countAbove(new int[]{}, 4) should return 0: an empty list (rule 2)
- countAbove(new int[]{9, 2, 7}, 5) should return 2: 9 and 7 count, the first value as well as the last (rule 1)