Skip to content
ExamSpine

Lesson 5.6 · Write it

Count up in a list

A functionPython or Java7 tests, 4 of them hidden

On a phone? Put the lines in order instead

Write a function that returns a list of the whole numbers from 1 up to n, in order.

  1. n is between 0 and 50.
  2. When n is 0, return an empty list.
  3. In Java, return an ArrayList<Integer>.

Only the list your function returns is checked.

def count_up(n: int) -> list[int]

Examples

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

  • count_up(4) should return [1, 2, 3, 4]: four numbers, starting at 1 and ending at n
  • count_up(0) should return []: n is 0, so the list is empty (rule 2)
  • count_up(1) should return [1]: one number, and it is n itself

public static ArrayList<Integer> countUp(int n)

Checking whether Java answers can run here.

Examples

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

  • countUp(4) should return [1, 2, 3, 4]: four numbers, starting at 1 and ending at n
  • countUp(0) should return []: n is 0, so the list is empty (rule 2)
  • countUp(1) should return [1]: one number, and it is n itself
solution.pySolution.java