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.
nis between 0 and 50.- When
nis 0, return an empty list. - 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)
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 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