Skip to content
ExamSpine

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.

  1. Only values strictly greater than limit count. A value equal to limit does not.
  2. An empty list returns 0.
  3. 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)

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)
solution.pySolution.java